Skip to content

fix(harness): keep memory flush/consolidation IO off the per-call sandbox - #3113

Merged
jujn merged 2 commits into
agentscope-ai:mainfrom
birdie7761:fix/3110-memory-flush-sandbox-independent
Oct 29, 2026
Merged

jujn merged 2 commits into
agentscope-ai:mainfrom
birdie7761:fix/3110-memory-flush-sandbox-independent

Conversation

@birdie7761

@birdie7761 birdie7761 commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3110

Description

The fire-and-forget memory flush (and the periodic consolidation) run after the agent event stream terminates — by then the sandbox lease may already be released, and every write through SandboxBackedFilesystem fails with No active sandbox — sandbox filesystem used outside of a call context. Per-call memory extraction was silently lost in sandbox-backed deployments.

Memory files (MEMORY.md, memory/*.md, the memory state) are cross-call metadata: like task records (#2803), their IO must not depend on a per-call sandbox instance. Following option (b) recorded on the issue (sandbox-independent storage), applied to the whole memory family so listing, reads and writes can never split between two stores:

  • WorkspaceManager gains memory-scoped IO (readMemoryFileUtf8 / appendMemoryFileUtf8 / writeMemoryFileUtf8) that routes to the host workspace whenever the filesystem serving the path is the per-call sandbox proxy; RoutedSandboxFilesystem prefix routes still take over. readMemoryMd delegates to the routed read so a host-side write is never shadowed by a stale sandbox copy.
  • User isolation is preserved: the host-side location is the context's runtime-data namespace (resolveRuntimeDataPath) — the same namespaced layout RemoteFilesystem applies to memory paths in distributed deployments. With no namespace factory the paths resolve to the workspace root, the location the no-filesystem mode has always used.
  • listMemoryFileInfos is the single home for memory listing: under a sandbox-backed filesystem it enumerates the namespaced host memory directory (real sizes and mtimes); otherwise it globs the filesystem layer. The unified probe resolves RoutedSandboxFilesystem configurations (a bare instanceof in the consolidator would misroute them).
  • MemoryFlushManager reads existing content and appends the daily ledger; MemoryConsolidator lists ledgers via the unified listing and rewrites MEMORY.md and the state watermark — read/write symmetric on the same store (an asymmetric watermark pair would silently re-merge all historical ledgers every run).
  • The memory tools join the same store: memory_save appends and memory_get / memory_search read through the memory-scoped IO, and the search listing observes the same namespaced scope. Without this, tools would write into the per-lease sandbox copy while flush appends host-side — splitting one day's ledger across two stores and keeping tool-written memories out of consolidation forever.

⚠️ Breaking Change (sandbox-backed deployments only)

Memory files move from per-lease sandbox snapshots to the host workspace. Local and remote filesystem modes are unaffected.

  • Reads: readMemoryMd and memory tools no longer consult the sandbox layer — host-side copy is authoritative.
  • Writes: flush, consolidation, and memory_save all write host-side instead of into the sandbox.
  • Location: workspace/<ns>/MEMORY.md, workspace/<ns>/memory/*.md (namespace omitted when no NamespaceFactory).

Upgrade: memory inside old sandbox snapshots becomes invisible. Before upgrading, copy MEMORY.md and memory/*.md from the sandbox snapshot to the host workspace path above. After upgrading, watch for WARN logs naming files that exist sandbox-side but not host-side — those need manual migration.

Justification: the bug (#3110) already silently lost all post-stream memory writes in sandbox mode. This makes memory persistent across calls; the only net-new loss is in-call memory_save writes that previously landed in the sandbox.

No file overlap with #3094 (sandbox-mirror release path) or #3106 (transcript quiescence) — this fix composes with both.

Test evidence (TDD)

New MemoryFlushSandboxIndependenceTest (7 cases), red on main for the three original cases:

Case Evidence
flush after stream ends writes the daily ledger host-side red on main: no file lands (write swallowed); green: host file with content
consolidation reads ledgers and rewrites MEMORY.md host-side red on main: throws through the dead proxy; green: host rewrite
MEMORY.md readable under a dead sandbox proxy red: unreachable; green: host copy read
memory_save lands host-side, immediately readable by the family no split storage
RoutedSandboxFilesystem-wrapping-dead-sandbox consolidates the bare-instanceof misroute would be silent
namespaced deployment keeps real isolation file lands under workspace/<ns>/memory, no bare-root copy
watermark persists — second consolidation is a no-op counting model called exactly once across two runs

Full harness suite: only pre-existing failures remain (AguiPermissionResumeTest 4/4 fails identically on pristine main — stash-verified; honorsParentMemoryConfig pair and the DynamicHookBuilder teardown are timing flakes that pass in isolation/re-run).

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (class/method javadocs document the routing, the isolation contract, and the deprecated-free getMemoryDir contract)
  • Code is ready for review

@codecov

codecov Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.56863% with 29 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...cope/harness/agent/workspace/WorkspaceManager.java 69.66% 15 Missing and 12 partials ⚠️
...o/agentscope/harness/agent/tool/MemoryGetTool.java 0.00% 1 Missing ⚠️
...gentscope/harness/agent/tool/MemorySearchTool.java 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Fixes #3110 by taking the memory family off the per-call sandbox proxy: WorkspaceManager gains memory-scoped read / append / write that route to the host runtime-data namespace when the serving filesystem is the sandbox, listMemoryFileInfos becomes the single listing home, and flush, consolidation, the watermark and the three memory tools all go through it. The diagnosis is right and the design is the correct one — reads and writes are symmetric, the routed configuration is resolved by the unified probe instead of a bare instanceof, and the 7 new cases in MemoryFlushSandboxIndependenceTest (three of which you show red on main) cover exactly the paths that regressed. Holding at COMMENT because build (ubuntu-latest) and build (windows-latest) are still pending on 1f11efe7 and mergeStateStatus is BLOCKED; the three inline notes are about storage assumptions rather than defects in the routing code.

Findings

  • [Warning] WorkspaceManager.java:280 — host-side is only equivalent to the old store if the host workspace is shared across replicas; on a per-pod workspace this trades a failed post-stream write for memory that the next replica cannot see. Needs a stated durability assumption (or the mirror path used for transcripts/session trees).
  • [Warning] WorkspaceManager.java:1051 — listing probes the memory directory while MEMORY.md, the ledgers and the watermark each probe their own path, so a memory/ prefix route splits the family across two stores (ledgers on the routed backend, MEMORY.md host-side) and listMemoryFilePaths still reads MEMORY.md through the sandbox proxy — the same post-lease path #3110 is about. Resolve the decision once per family and add a routed-prefix test.
  • [Warning] WorkspaceManager.java:272 — on upgrade the host copy does not exist yet while the old sandbox copy does, so the first call after deployment starts from empty memory and the flush cements that. Consider a one-shot adoption, or at least a log line when a memory path is absent host-side but present in the sandbox.
  • [Info] WorkspaceManager.java:547 — memoryFilesRouteToLiveSandbox and taskRecordsRouteToLiveSandbox are now identical probes; either one method for both families, or per-family resolution as asked above.

Tests

Coverage is strong for the sandbox case. The two gaps map to the findings: no case where memory/ and MEMORY.md route to different backends, and no case for upgrading from a deployment whose memory only exists in an old sandbox snapshot.

Cross-repo Note

The durability assumption above is the same one #3094 (sandbox-mirror release path) and #3106 (transcript quiescence) have to answer; if memory now writes host-side while those still mirror through the sandbox, the three paths need one documented rule about what lives where.


Automated review by github-manager-bot

…dbox

The fire-and-forget memory flush (and the periodic consolidation) run
AFTER the agent event stream terminates - by then the sandbox lease may
already be released, and every write through SandboxBackedFilesystem
failed with "No active sandbox - sandbox filesystem used outside of a
call context" (agentscope-ai#3110). Per-call memory extraction was silently lost in
sandbox-backed deployments.

Memory files (MEMORY.md, memory/*.md, the memory state) are cross-call
metadata: like task records (agentscope-ai#2803), their IO must not depend on a
per-call sandbox instance. Following the direction recorded on the
issue (option b - sandbox-independent storage), applied to the WHOLE
memory family so listing, reads and writes can never split between two
stores:

- WorkspaceManager gains memory-scoped IO (readMemoryFileUtf8 /
  appendMemoryFileUtf8 / writeMemoryFileUtf8) that routes to the host
  workspace whenever the filesystem serving the path is the per-call
  sandbox proxy (RoutedSandbox prefix routes still take over).
  readMemoryMd delegates to the routed read so a host-side write is
  never shadowed by a stale sandbox copy.
- USER ISOLATION IS PRESERVED: the host-side location is the context's
  runtime-data namespace (resolveRuntimeDataPath) - the same namespaced
  layout RemoteFilesystem applies to memory paths in distributed
  deployments. Memory files have no per-user keying in their names
  (MEMORY.md, memory/<date>.md), so the directory namespace is their
  only isolation mechanism; a multi-user workspace keeps per-user
  ledgers exactly as the remote-fs mode always has. With no namespace
  factory configured the paths resolve to the workspace root - the
  location the no-filesystem mode has always used.
- listMemoryFileInfos: the single home for memory listing. Under a
  sandbox-backed filesystem it enumerates the context's namespaced host
  memory directory with real sizes and mtimes; otherwise it globs the
  filesystem layer. The unified probe resolves RoutedSandboxFilesystem
  configurations (a bare instanceof in the consolidator would misroute
  them).
- MemoryFlushManager reads existing content and appends the daily
  ledger through the memory-scoped IO; MemoryConsolidator delegates its
  ledger listing entirely to listMemoryFileInfos and rewrites MEMORY.md
  and the state watermark through the memory-scoped IO.
- The memory TOOLS join the same store: memory_save appends and
  memory_get / memory_search read through the memory-scoped IO, and
  memory_search's listing (listMemoryFilePaths) observes the same
  namespaced scope in its local fallback. Without this the tools would
  keep writing into the per-lease sandbox copy while flush appends
  host-side - splitting one day's ledger across two stores and keeping
  tool-written memories out of consolidation forever.
- The shared sandbox-path probe is factored out of the task-record
  routing so both families use one definition.

Behavior-change disclosure (sandbox-backed deployments only; local and
remote filesystem modes are untouched): memory files previously lived
inside per-lease sandbox snapshots, where the post-stream flush could
not write at all (the bug) and in-call tool writes died with the lease.
From now on the authoritative copies live host-side under the
runtime-data namespace; memory accumulated inside old sandbox snapshots
becomes invisible (it was already unwritable post-stream, so the loss
surface is the failed writes themselves).

TDD red on main (3 initial cases: no daily ledger lands, consolidation
throws through the dead proxy, MEMORY.md unreachable); green after the
routing. Review-round additions: memory_save lands host-side and is
immediately readable back by the rest of the family (no split storage);
a RoutedSandboxFilesystem-wrapping-dead-sandbox configuration
consolidates correctly; and a namespaced-deployment case (non-null
NamespaceFactory) asserts BOTH search discoverability AND real
isolation - the daily ledger lands under workspace/<ns>/memory with no
bare-root copy. Full harness suite: only pre-existing failures remain
(AguiPermissionResumeTest 4/4 fails identically on pristine main -
stash-verified; the known flake family covers the rest).

Maintainer-bot review round (per-member anchoring): the search
listing's single directory-anchored guard sent MEMORY.md's existence
probe through the dead sandbox proxy whenever a memory/ prefix route
was mounted (MEMORY.md is root-level and never matches that route, so
its serving backend stayed the per-call sandbox). listMemoryFilePaths
now anchors each family member on the backend serving its own path:
the MEMORY.md existence check skips the sandbox proxy entirely (its
authoritative copy is host-side), while the ledger glob follows the
memory/ directory's backend (a mounted store serves it) - a routed
daily ledger stays discoverable and MEMORY.md never routes post-lease
IO through the proxy. Upgrade observability: a host-side read that
misses while the sandbox layer still holds a copy logs a warning (the
stale copy is deliberately not adopted - every post-stream write was
already lost from it); post-lease the probe fails fast and is skipped
silently. The durability assumption for stateless multi-replica
deployments is now stated on the family read (whole family on a shared
persistent backend: non-sandbox primary, or routes covering both
memory/ and MEMORY.md). Two new cases: the routed-prefix family split
(red on the old guard - it threw through the dead proxy) and the
upgrade shape (no adoption; exactly one sandbox interaction on a host
miss).

Fixes agentscope-ai#3110
@birdie7761
birdie7761 force-pushed the fix/3110-memory-flush-sandbox-independent branch from 1f11efe to df3e8e1 Compare September 12, 2026 15:16
oss-maintainer

This comment was marked as abuse.

@jujn
jujn merged commit 269ccd1 into agentscope-ai:main Oct 29, 2026
6 checks passed
DennisWLX added a commit to DennisWLX/agentscope-java that referenced this pull request Oct 29, 2026
Use the memory-specific reader introduced by upstream agentscope-ai#3113 in both memory search fixtures. Preserve the success, error-looking content and no-match assertions exposed by the latest PR merge CI.
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
…dbox (#3113)

Backport the verified fix to the 2.0.x implementation and retain its existing public contracts where the upstream kernel differs.

(cherry picked from commit 269ccd1)
Backport-review-patch: 7c9ecbdcd4acc4db8bad06af1daca708c2d8aadd
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
jujn pushed a commit that referenced this pull request Nov 28, 2026
## Description

Fixes #4257.

Daily-memory expiration still accesses the sandbox filesystem directly
after the sandbox has been released. Follow the memory I/O approach
introduced in #3113: route listing and archival moves through
`WorkspaceManager`, using the authoritative host memory namespace for
sandbox-backed paths and retaining native move behavior for persistent
filesystem routes.

- Preserve the calling user's runtime-data namespace, existing path
normalization, replacement semantics and backend failure results.
- Process only direct date-named ledgers, not archived or nested entries
returned by recursive glob implementations.
- Keep per-file best-effort maintenance and report an archive only after
a successful move. Handle null/failed/empty listings without preventing
consolidation.
- Do not change sandbox release timing or lifecycle policy.
Cross-backend host/persistent moves return a failure instead of
copying/deleting between stores.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants