Skip to content

Commit a199ec7

Browse files
committed
fix(harness): close the hidden subtree to every layer, at any depth
The guard only reached the layers that excluded the root, so anything routed above it walked in: grep, glob, ls and delete read or removed a sibling's files through a link, a junction, an 8.3 short name or an absolute ancestor, and one caller could unlink another namespace's link entry. Every layer now keeps its guarded roots as a single set, spelled and resolved, with the bound carried per root: HIDDEN loses the content, REFUSED closes the way in from above while the layer that owns the subtree still reads inside. A walk scope resolves its root's position once, so an entry that reaches a guarded root only through an alias is recognised at any depth instead of by the file name it happens to keep, and a walk that starts inside such a path answers empty before a shelled-out search can look at it. The overlay keeps its routing rules but gains the spellings it dropped: an absolute path naming the workspace root or the caller's namespace root now resolves to that root instead of falling into the project view, and an absolute path that reaches the workspace only through a link is answered by the project view, which hides the subtree, instead of surfacing a namespace error. A path that lands in the caller's own namespace keeps the workspace view, so a link into one's own files stays readable. Physical resolution moved to FilesystemUtils so both layers share it. Reads also had no way to see what the overlay writes: with namespaces active a non-workspace write landed under the caller's project namespace, while every read looked at the unnamespaced project, so exists, read, ls, grep and glob all reported the file missing -- a success the caller could never observe. Reads now check the workspace, then the project write layer, then the shared project, and leave an absolute project path to the project layers: the workspace layer used to answer for files above its own root under a "../" spelling, so the same file came back twice under two keys. Listing ops also disagreed with the read ops about a path a layer refuses: the overlay let the refusal escape, so a sibling's absolute path listed as a SecurityException while the same path read as absent, and the plain overlay did that where the project-writable one answered empty. Enumeration now treats a layer that refuses the path as contributing nothing, which is what read, exists and grep already did. Walks also disagreed with resolution about where an entry sits. A refused root closed itself for the layer that owns it, so a caller whose namespace list is empty -- the AGENT and GLOBAL scopes -- listed nothing while reading the same key still worked. A directory alias that is not a symbolic link, such as a Windows junction, passed the link check and was walked into, so a sibling's names showed up in listings that would refuse to read them. A refused root now closes only from above, and an entry inside the layer yet outside the caller's namespace is skipped however the walk reached it. A refused root also stays walkable for the layer rooted in it when the deployment spells that root through an alias, since the refusal belongs to the layer that lives there, not to the spelling. A link to an ancestor could also be deleted but not moved: a moving link carries nothing, and the directory flag is now read without following it. The classifier and the router also read spellings differently: ./MEMORY.md was workspace metadata to one and a project key to the other, so the write landed in the project layer while the read found it there. Keys are normalized the same way in both. Finally, an alias may no longer carry a spelling the path policy excludes into a place the policy allows. Guarding is a path model, and the class doc now says where that ends: a hard link is a second directory entry for the same file, not a pointer, so it is invisible to a resolution-based check, and is documented as outside the boundary rather than half-detected. A layered write could also leave the caller reading a version nobody else sees. Reads ask the workspace layer first, so a project write under a key the workspace already holds would succeed and leave the caller reading the other file. That write, its edit, an upload and a move are now refused, naming the workspace copy with its stamp and size; absolute spellings are exempt, because there the workspace layer answers only through the shared path policy. Deleting the key removes the workspace copy first, so the refusal's recovery path converges. Listings merged on the raw spelling, which the layers do not agree on -- a virtual layer reports /src/ and a rooted one src/ -- so the same entry appeared twice and the upper copy never took precedence. Merges now key on a normalized entry. Search resolves each directory once per search instead of once per reported line. The dev filesystem guide notes the shadowed project write and that the shell and hard links sit outside the boundary; the 2.0 snapshot is left to the release it documents. An alias check compared a resolved path against the path policy's configured roots. On Windows a short-name spelling resolves to a different, longer form, so that comparison failed and the request fell through to the unconditional policy branch, skipping the namespace check and reading a sibling's file through the alias. The branch now checks the caller's spelling against the policy and uses the resolved path only to decide which layer owns the destination.
1 parent 21be74a commit a199ec7

12 files changed

Lines changed: 2273 additions & 119 deletions

File tree

‎agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/AbstractFilesystem.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.agentscope.harness.agent.filesystem.local.LocalFilesystemWithShell;
2121
import io.agentscope.harness.agent.filesystem.model.EditResult;
2222
import io.agentscope.harness.agent.filesystem.model.FileDownloadResponse;
23+
import io.agentscope.harness.agent.filesystem.model.FileInfo;
2324
import io.agentscope.harness.agent.filesystem.model.FileUploadResponse;
2425
import io.agentscope.harness.agent.filesystem.model.GlobResult;
2526
import io.agentscope.harness.agent.filesystem.model.GrepResult;
@@ -28,6 +29,7 @@
2829
import io.agentscope.harness.agent.filesystem.model.WriteResult;
2930
import java.util.List;
3031
import java.util.Map;
32+
import java.util.Optional;
3133

3234
/**
3335
* Abstract filesystem API for agents: list, read, write, edit, grep, glob, upload, download.
@@ -174,6 +176,20 @@ List<FileUploadResponse> uploadFiles(
174176
*/
175177
boolean exists(RuntimeContext runtimeContext, String path);
176178

179+
/**
180+
* Metadata for one existing path: size, directory flag and modification time.
181+
*
182+
* <p>Layers that only stream bytes can let the default answer empty, which callers treat as
183+
* "this layer has no opinion" rather than "the path is absent".
184+
*
185+
* @param runtimeContext per-call agent runtime; {@link RuntimeContext#empty()} when none
186+
* @param path path to describe, in the same spelling {@link #ls} accepts
187+
* @return metadata, or empty when the layer has no such path or cannot report it
188+
*/
189+
default Optional<FileInfo> stat(RuntimeContext runtimeContext, String path) {
190+
return Optional.empty();
191+
}
192+
177193
// ==================== Path validation utility ====================
178194

179195
/**

‎agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/OverlayFilesystem.java‎

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.agentscope.harness.agent.filesystem.model.ReadResult;
2929
import io.agentscope.harness.agent.filesystem.model.WriteResult;
3030
import io.agentscope.harness.agent.filesystem.sandbox.AbstractSandboxFilesystem;
31+
import io.agentscope.harness.agent.filesystem.util.FilesystemUtils;
3132
import java.util.ArrayList;
3233
import java.util.LinkedHashMap;
3334
import java.util.List;
@@ -44,14 +45,17 @@
4445
* <p>Merge semantics:
4546
*
4647
* <ul>
47-
* <li>{@code ls} — union of both layers; upper entries take precedence on path collision
48+
* <li>{@code ls} — union of both layers; upper entries take precedence on path collision, which
49+
* {@link FilesystemUtils#entryKey} decides because the layers spell one entry differently (a
50+
* leading {@code /}, a trailing {@code /} on directories)
4851
* <li>{@code read} — upper first, then lower
4952
* <li>{@code write}/{@code edit} — always to upper (copy-on-write)
5053
* <li>{@code delete} — removes from upper only; shared-layer files cannot be deleted
5154
* <li>{@code grep}/{@code glob} — searches both layers; upper results override lower on path
5255
* collision
5356
* <li>{@code exists} — true if present in either layer
5457
* </ul>
58+
*
5559
*/
5660
public class OverlayFilesystem implements AbstractFilesystem {
5761

@@ -140,24 +144,65 @@ public ExecuteResponse execute(
140144
}
141145
}
142146

147+
/*
148+
* Enumeration answers from a layer that refuses the path outright are treated as "nothing to
149+
* contribute". This view is a mount over its layers, so a path one layer does not let the
150+
* caller see is absent from that layer rather than an error for the caller: read, exists and
151+
* grep already answer that way, and only the listing ops let the refusal escape, where the
152+
* same location then listed as an exception but read as not found.
153+
*/
154+
155+
private static LsResult lsOrNothing(
156+
AbstractFilesystem layer, RuntimeContext runtimeContext, String path) {
157+
try {
158+
return layer.ls(runtimeContext, path);
159+
} catch (SecurityException invisible) {
160+
return LsResult.success(List.of());
161+
}
162+
}
163+
164+
private static GrepResult grepOrNothing(
165+
AbstractFilesystem layer,
166+
RuntimeContext runtimeContext,
167+
String pattern,
168+
String path,
169+
String glob) {
170+
try {
171+
return layer.grep(runtimeContext, pattern, path, glob);
172+
} catch (SecurityException invisible) {
173+
return GrepResult.success(List.of());
174+
}
175+
}
176+
177+
private static GlobResult globOrNothing(
178+
AbstractFilesystem layer, RuntimeContext runtimeContext, String pattern, String path) {
179+
try {
180+
return layer.glob(runtimeContext, pattern, path);
181+
} catch (SecurityException invisible) {
182+
return GlobResult.success(List.of());
183+
}
184+
}
185+
143186
@Override
144187
public LsResult ls(RuntimeContext runtimeContext, String path) {
145-
LsResult upperResult = upper.ls(runtimeContext, path);
146-
LsResult lowerResult = lower.ls(runtimeContext, path);
188+
LsResult upperResult = lsOrNothing(upper, runtimeContext, path);
189+
LsResult lowerResult = lsOrNothing(lower, runtimeContext, path);
147190

148191
if (!upperResult.isSuccess() && !lowerResult.isSuccess()) {
149192
return lowerResult;
150193
}
151194

195+
// Keyed by entry, not by spelling: the layers disagree on the leading slash, so the raw
196+
// path listed one entry twice and the upper layer never took precedence.
152197
Map<String, FileInfo> merged = new LinkedHashMap<>();
153198
if (lowerResult.isSuccess() && lowerResult.entries() != null) {
154199
for (FileInfo fi : lowerResult.entries()) {
155-
merged.put(fi.path(), fi);
200+
merged.put(FilesystemUtils.entryKey(fi.path()), fi);
156201
}
157202
}
158203
if (upperResult.isSuccess() && upperResult.entries() != null) {
159204
for (FileInfo fi : upperResult.entries()) {
160-
merged.put(fi.path(), fi);
205+
merged.put(FilesystemUtils.entryKey(fi.path()), fi);
161206
}
162207
}
163208
return LsResult.success(new ArrayList<>(merged.values()));
@@ -200,8 +245,8 @@ public EditResult edit(
200245
@Override
201246
public GrepResult grep(
202247
RuntimeContext runtimeContext, String pattern, String path, String glob) {
203-
GrepResult upperResult = upper.grep(runtimeContext, pattern, path, glob);
204-
GrepResult lowerResult = lower.grep(runtimeContext, pattern, path, glob);
248+
GrepResult upperResult = grepOrNothing(upper, runtimeContext, pattern, path, glob);
249+
GrepResult lowerResult = grepOrNothing(lower, runtimeContext, pattern, path, glob);
205250

206251
if (!upperResult.isSuccess() && !lowerResult.isSuccess()) {
207252
return lowerResult;
@@ -210,21 +255,21 @@ public GrepResult grep(
210255
Map<String, GrepMatch> merged = new LinkedHashMap<>();
211256
if (lowerResult.isSuccess() && lowerResult.matches() != null) {
212257
for (GrepMatch m : lowerResult.matches()) {
213-
merged.put(m.path() + ":" + m.line(), m);
258+
merged.put(FilesystemUtils.entryKey(m.path()) + ":" + m.line(), m);
214259
}
215260
}
216261
if (upperResult.isSuccess() && upperResult.matches() != null) {
217262
for (GrepMatch m : upperResult.matches()) {
218-
merged.put(m.path() + ":" + m.line(), m);
263+
merged.put(FilesystemUtils.entryKey(m.path()) + ":" + m.line(), m);
219264
}
220265
}
221266
return GrepResult.success(new ArrayList<>(merged.values()));
222267
}
223268

224269
@Override
225270
public GlobResult glob(RuntimeContext runtimeContext, String pattern, String path) {
226-
GlobResult upperResult = upper.glob(runtimeContext, pattern, path);
227-
GlobResult lowerResult = lower.glob(runtimeContext, pattern, path);
271+
GlobResult upperResult = globOrNothing(upper, runtimeContext, pattern, path);
272+
GlobResult lowerResult = globOrNothing(lower, runtimeContext, pattern, path);
228273

229274
if (!upperResult.isSuccess() && !lowerResult.isSuccess()) {
230275
return lowerResult;
@@ -233,12 +278,12 @@ public GlobResult glob(RuntimeContext runtimeContext, String pattern, String pat
233278
Map<String, FileInfo> merged = new LinkedHashMap<>();
234279
if (lowerResult.isSuccess() && lowerResult.matches() != null) {
235280
for (FileInfo fi : lowerResult.matches()) {
236-
merged.put(fi.path(), fi);
281+
merged.put(FilesystemUtils.entryKey(fi.path()), fi);
237282
}
238283
}
239284
if (upperResult.isSuccess() && upperResult.matches() != null) {
240285
for (FileInfo fi : upperResult.matches()) {
241-
merged.put(fi.path(), fi);
286+
merged.put(FilesystemUtils.entryKey(fi.path()), fi);
242287
}
243288
}
244289
return GlobResult.success(new ArrayList<>(merged.values()));

0 commit comments

Comments
 (0)