agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]:MarketplaceStager orphan GC races with concurrent staging —UncheckedIOException(NoSuchFileException) escapes and fails the whole agent call

Open huang4953 opened this issue 3 months ago • 0 comments

Describe the bug

When multiple HarnessAgent calls share one workspace root but have different visible skill sets per call (e.g. per-user skill visibility), every call runs HarnessSkillMiddleware.onSystemPrompt → MarketplaceStager.stage(...), which ends with garbageCollectOrphans() deleting cached skill directories that are not in this call's retained set.

Two defects combine to fail the entire agent call:

  1. Exception-handling gap. garbageCollectOrphans / deleteRecursively / removeUnexpected traverse directories with Files.walk(...), whose lazy stream throws UncheckedIOException (a RuntimeException) when an entry disappears mid-iteration. All three methods only catch (IOException ...), so the wrapped NoSuchFileException escapes. For per-skill materializeIfChanged the outer catch (Exception) in stage() contains the damage, but garbageCollectOrphans(cacheRoot, retained) is called outside that try-catch, so any failure there propagates up through the middleware chain and terminates the agent execution.

  2. Per-call GC on a shared cache. The retained white-list is rebuilt from a single call's visible skills. With per-user visibility, a skill directory legitimately staged by concurrent call A is an "orphan" for call B and gets recursively deleted — potentially while A is walking/materializing the same tree (or vice versa). There is no grace period, reference counting, or namespace isolation for .skills-cache (unlike runtime data paths, which are prefixed via NamespaceFactory/IsolationScope).

To Reproduce

Steps to reproduce the behavior:

  1. Build a HarnessAgent with .workspace(<sharedRoot>) and a skill repository whose visible skill set varies per call (e.g. filtered by RuntimeContext.getUserId()), where at least one skill contains a subdirectory (e.g. scripts/).
  2. Run two concurrent calls against the same workspace root where the skill is visible to call A but not to call B (sandbox disabled, plain local workspace).
  3. Call B's stage() reaches garbageCollectOrphans and deleteRecursively(skillDir) while call A is concurrently staging/walking the same skill directory (or call A's removeUnexpected prunes a subdirectory another call is enumerating).

Expected behavior

  • Directory traversal during staging/GC should tolerate entries deleted by concurrent calls (skip vanished paths instead of failing).
  • Orphan GC on a shared workspace cache should not delete directories that other in-flight calls may still use (e.g. mtime grace period, ref-counting, or per-isolation-scope cache namespaces consistent with resolveRuntimeDataPath).
  • At minimum, a staging/GC failure should degrade gracefully (as the per-skill catch (Exception) already does), never fail the whole agent call.

Error messages

ERROR ... UncheckedIOException: java.nio.file.NoSuchFileException:
  <workspace>/.skills-cache/<source-ns>/<skill-name>/scripts

Observed production signature: the call fails within ~3 s, before the first model round completes (totalEvents=1, no tool execution). The failing path is always a subdirectory of a staged skill that was being deleted concurrently. No sandbox involved (filesystem/shell tools disabled, local workspace only).

Environment (please complete the following information):

  • AgentScope-Java Version: 2.0.1 (agentscope-harness)
  • Java Version: 17
  • OS: Linux (production, shared workspace volume; also reproducible single-node with concurrent threads)

Additional context

Suggested fixes (any combination):

  1. Widen the catch scope in MarketplaceStager: garbageCollectOrphans / deleteRecursively / removeUnexpected should catch RuntimeException (or explicitly UncheckedIOException) and log-and-continue, matching the per-skill fault tolerance in stage().
  2. Make deleteRecursively race-safe: iterate with deleteIfExists and tolerate NoSuchFileException per entry instead of relying on a single Files.walk snapshot.
  3. Design-level: don't GC a shared cache based on one call's visibility snapshot — add a grace period (delete only entries untouched for N minutes) or prefix .skills-cache with the same namespace IsolationScope applies to runtime data, so different users never stage/GC into the same tree.

Relevant code (v2.0.1): agentscope-harness/.../skill/runtime/MarketplaceStager.java — stage(), garbageCollectOrphans(), deleteRecursively(), removeUnexpected(); trigger path: HarnessSkillMiddleware.onSystemPrompt().

huang4953 avatar Aug 20 '26 02:08 huang4953