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 @@ -144,7 +144,7 @@ public static Builder builder() {
*
* <p>Note: The input map structure is copied so that entries can be modified
* independently, but nested values (typed as Object) remain shared.
* Immutable fields (toolUseBlock, agent, context, emitter) are shared by reference.
* Immutable fields (toolUseBlock, agent, runtimeContext, emitter) are shared by reference.
*
* @param source The existing ToolCallParam to copy values from
* @return A new builder pre-populated with the source's values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ void testCopyAllFields() {
assertEquals(original.getInput(), copy.getInput());
assertEquals(original.getAgent(), copy.getAgent());
assertSame(original.getRuntimeContext(), copy.getRuntimeContext());
assertNotNull(copy.getContext());
assertEquals(original.getContext().get(String.class), copy.getContext().get(String.class));
assertSame(original.getEmitter(), copy.getEmitter());
}

Expand Down Expand Up @@ -249,6 +251,7 @@ void testCopyWithNullFields() {
assertEquals(original.getToolUseBlock(), copy.getToolUseBlock());
assertTrue(copy.getInput().isEmpty());
assertNull(copy.getAgent());
assertNull(copy.getRuntimeContext());
assertNull(copy.getContext());
}

Expand Down Expand Up @@ -288,6 +291,8 @@ void testPreserveImmutableFields() {
assertSame(original.getToolUseBlock(), copy.getToolUseBlock());
assertSame(original.getAgent(), copy.getAgent());
assertSame(original.getRuntimeContext(), copy.getRuntimeContext());
assertNotNull(copy.getContext());
assertEquals(original.getContext().get(String.class), copy.getContext().get(String.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public List<Path> listKnowledgeFiles(RuntimeContext rc) {
if (glob.isSuccess() && glob.matches() != null) {
for (FileInfo fi : glob.matches()) {
if (fi.path() != null && !fi.path().isBlank()) {
relativePaths.add(normalizeRelativePath(fi.path().trim()));
relativePaths.add(normalizeListedPath(fi.path().trim()));
}
}
}
Expand Down Expand Up @@ -540,7 +540,10 @@ public Collection<TaskRecord> listAllTaskRecords(
if (fi.path() == null || fi.path().isBlank()) {
continue;
}
String rel = normalizeRelativePath(fi.path().trim());
String rel = normalizeListedPath(fi.path().trim());
if (rel.isEmpty()) {
continue;
}
Instant mtime = parseInstantQuiet(fi.modifiedAt());
relPaths.put(rel, Optional.ofNullable(mtime));
}
Expand Down Expand Up @@ -929,6 +932,36 @@ static String normalizeRelativePath(String relativePath) {
return s;
}

/**
* Normalizes a path returned by {@link AbstractFilesystem#glob} into a workspace-relative
* string when possible.
*/
private String normalizeListedPath(String path) {
if (path == null || path.isBlank()) {
return "";
}
String normalized = path.replace('\\', '/').strip();
Path workspaceAbs = workspace.toAbsolutePath().normalize();
try {
Path candidate = Path.of(normalized).normalize();
if (candidate.isAbsolute()) {
if (candidate.startsWith(workspaceAbs)) {
return workspaceAbs.relativize(candidate).toString().replace('\\', '/');
}
if (normalized.startsWith("/")) {
return normalized.substring(1);
}
return normalized;
}
} catch (Exception ignored) {
// Fall through to the string-based fallback below.
}
if (normalized.startsWith("/")) {
return normalized.substring(1);
}
return normalized;
}

/**
* Returns workspace-relative paths of all memory files ({@code MEMORY.md} and {@code
* memory/*.md}). Unions results from the {@link AbstractFilesystem} layer and the local disk,
Expand All @@ -946,7 +979,7 @@ public List<String> listMemoryFilePaths(RuntimeContext rc) {
if (glob.isSuccess() && glob.matches() != null) {
for (FileInfo fi : glob.matches()) {
if (fi.path() != null && !fi.path().isBlank()) {
String rel = normalizeRelativePath(fi.path().trim());
String rel = normalizeListedPath(fi.path().trim());
if (!rel.isEmpty()) {
paths.add(rel);
}
Expand Down Expand Up @@ -983,7 +1016,7 @@ public List<String> listSessionLogFiles(RuntimeContext rc) {
if (glob.isSuccess() && glob.matches() != null) {
for (FileInfo fi : glob.matches()) {
if (fi.path() != null && !fi.path().isBlank()) {
String rel = normalizeRelativePath(fi.path().trim());
String rel = normalizeListedPath(fi.path().trim());
if (!rel.isEmpty()) {
paths.add(rel);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.workspace;

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.AbstractFilesystem;
import io.agentscope.harness.agent.filesystem.spec.LocalFilesystemSpec;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

class WorkspaceManagerListingTest {

@Test
void listKnowledgeFiles_returnsWorkspaceRelativePaths(
@TempDir Path project, @TempDir Path workspace) throws IOException {
Files.createDirectories(workspace.resolve("knowledge"));
Files.writeString(workspace.resolve("knowledge/guide.md"), "guide");

AbstractFilesystem fs = new LocalFilesystemSpec().project(project).toFilesystem(workspace, null);
try (WorkspaceManager wm = new WorkspaceManager(workspace, fs)) {
List<Path> knowledgeFiles = wm.listKnowledgeFiles(RuntimeContext.empty());
assertEquals(1, knowledgeFiles.size(), () -> "Unexpected knowledge files: " + knowledgeFiles);
assertEquals(
workspace.resolve("knowledge/guide.md").normalize(),
knowledgeFiles.get(0).normalize());
}
}

@Test
void listMemoryFilePaths_returnsWorkspaceRelativePaths(
@TempDir Path project, @TempDir Path workspace) throws IOException {
Files.writeString(workspace.resolve("MEMORY.md"), "memory");
Files.createDirectories(workspace.resolve("memory"));
Files.writeString(workspace.resolve("memory/notes.md"), "notes");

AbstractFilesystem fs = new LocalFilesystemSpec().project(project).toFilesystem(workspace, null);
try (WorkspaceManager wm = new WorkspaceManager(workspace, fs)) {
List<String> memoryFiles = wm.listMemoryFilePaths(RuntimeContext.empty());
assertEquals(2, memoryFiles.size(), () -> "Unexpected memory files: " + memoryFiles);
assertTrue(memoryFiles.contains("MEMORY.md"));
assertTrue(memoryFiles.contains("memory/notes.md"));
}
}
}
Loading