[Bug]:现在的记忆是异步写入,会有问题,写入的时候沙箱已经关闭,就会警告memory flush failed: no active sandbox, sandbox filesystem used outside of call context
AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.
Describe the bug A clear and concise description of what the bug is.
To Reproduce Steps to reproduce the behavior:
- You code
- How to execute
- See error
Expected behavior A clear and concise description of what you expected to happen.
Error messages Detailed error messages.
Environment (please complete the following information):
- AgentScope-Java Version: [e.g. 1.0.12, check your pom.xml dependency version or run
mvn dependency:tree | grep agentscope-parent:pom(only mac/linux)] - Java Version: [e.g. 17]
- OS: [e.g. macos, windows]
Additional context Add any other context about the problem here.
Issue Evaluation
Category: bug | Status: Confirmed
Verified against current main (c5db8f72). The report matches the code: the memory flush is scheduled as a fire-and-forget background task, while the sandbox lease is released as soon as the agent event stream terminates — the async write can therefore run after the sandbox is gone and fail with No active sandbox — sandbox filesystem used outside of a call context.
Root Cause: MemoryFlushMiddleware hooks the flush in doOnComplete and subscribes the actual I/O on Schedulers.boundedElastic() (agentscope-harness/.../middleware/MemoryFlushMiddleware.java:159,203-211). The sandbox is released via Flux.using cleanup in HarnessAgent (HarnessAgent.java:994-1007), and SandboxLifecycleMiddleware.releaseForCall clears the SandboxAcquireResult context binding (SandboxLifecycleMiddleware.java:151-177). The later write through MemoryFlushManager.writeMemoryFiles → WorkspaceManager.appendUtf8WorkspaceRelative → SandboxBackedFilesystem.requireSandbox then throws (SandboxBackedFilesystem.java:239-255).
Impact: In sandbox-backed deployments, per-call memory extraction/consolidation can be silently lost after the call completes (memory ledgers, session offloads), with Memory flush failed warnings in logs.
Severity: high
Possible directions: (1) block on in-flight flush tasks before releasing the sandbox (e.g. drain the flush future inside Flux.using cleanup), or (2) copy the needed memory payload into a sandbox-independent buffer before flush so the persisted write no longer depends on the closed sandbox.
A maintainer replying /approve here will trigger generation of an automated fix proposal (spec + PR).
Automated evaluation by github-manager-bot
Adjacent-PR advisory (no change to the evaluation above)
Draft PR #3106 (fix(harness): avoid blocking stream completion on transcript persistence, author ningmao-hlyz, opened 2026-09-11) applies option (1) from the evaluation — keep sandbox-backed work in the stream until the async write finishes — but to transcript persistence, and it fixes #3059 rather than this issue. It touches MemoryBackgroundTasks, SessionTranscriptWriter and TranscriptMiddleware; no file overlap with the memory-flush path cited in the evaluation (MemoryFlushMiddleware, MemoryFlushManager, SandboxLifecycleMiddleware, HarnessAgent).
Consequence: the memory-flush race reported here stays unfixed even if #3106 merges. If #3106 lands first, a fix for this issue should reuse its MemoryBackgroundTasks quiescence pattern instead of adding a second drain mechanism.
This is supplementary context only — the evaluation and its proposed directions are unchanged.
Automated evaluation by github-manager-bot
Adjacent-PR collision advisory (no change to the evaluation above)
A duplicate-fix scan of all open PRs (self-authored PRs excluded, 104 open) found active work on the same sandbox-async-write-after-release race reported here:
- #3094
fix(harness): defer self-managed sandbox shutdown until session mirroring completes(authorxy-ygz, OPEN, MERGEABLE) — addsSandboxMirrorReleaseCoordinatorwith refcounted sandbox release and touches two of the exact classes cited in the root cause above:SandboxLifecycleMiddleware.javaandSandboxBackedFilesystem.java. It closes #3036 (the NPE variant of the same race); the memory-flush call path named above is not in its file set, so it does not close this issue — but direction (1) ("drain in-flight work before releasing the sandbox") is already implemented there for the session-mirror path. - #2833
fix(harness): support asynchronous memory flush(authorguslegend0510, OPEN, CONFLICTING) — editsMemoryFlushMiddleware.javaandHarnessAgent.javaand adds opt-inMemoryConfig.asyncFlush(true), i.e. it institutionalises the fire-and-forget flush that produces this failure without draining it before lease release. - #3039
fix(sandbox): avoid session mirrors after sandbox release(OPEN, MERGEABLE) closes the same #3036 as #3094 — the maintainers already have two competing shapes of the same fix. - #3106 (draft, #3059) — already noted in the previous advisory on this thread: same quiescence idea, transcript path only, no file overlap with the memory-flush path.
Consequence for the /approve offer above: a generated fix would collide in SandboxLifecycleMiddleware / SandboxBackedFilesystem / HarnessAgent with #3094 and #2833, which is exactly the duplicate-fix collision our supersession notices close windows elsewhere to avoid. We are not closing this window — none of the four PRs fixes the reported failure: #3094/#3039 drain only session-mirror tasks, #3106 only transcript writes, and #2833 makes the flush asynchronous without draining it before lease release — but we will not file a competing PR unless the community tells us which seam to build on.
Preferred decision for anyone replying: (a) extend #3094's SandboxMirrorReleaseCoordinator to cover memory-flush tasks, or (b) take direction (2) from the evaluation above — copy the payload into a sandbox-independent buffer before flush — which is collision-free against all four PRs. Option (b) is the only one that can land without a merge conflict against #3094/#2833.
This is supplementary context only — the evaluation and its proposed directions are unchanged, and the reply window stays open.
Automated evaluation by github-manager-bot
I support Option (b) — copying the memory payload into a sandbox-independent buffer before flush. This avoids conflicts with #3094 and #2833 while fixing the root cause.
Reply-window advisory — option preference recorded, no change to the evaluation above
Thanks @BEN5890 — noted that option (2)/(b) (snapshot the memory payload into a sandbox-independent buffer before the flush, instead of draining in-flight tasks before the lease release) is the preferred shape. That is the first reply on this window, and it is recorded as reporter preference, not as an approval to generate code: the offer in the evaluation comment above asks a maintainer to approve explicitly, so this window stays open unchanged.
Two collision facts refreshed against current main (c5db8f72), both of which bear on that choice:
- #3094 (
defer self-managed sandbox shutdown until session mirroring completes) is nowAPPROVEDandMERGEABLEwithmergeStateStatus=CLEANand all checks green (Check License,Check Module Sync,build (ubuntu-latest),build (windows-latest),codecov/patch). It closes #3036 only. Its file set is the sandbox-mirror release path, not the memory-flush path named in the evaluation (MemoryFlushMiddleware→MemoryFlushManager→WorkspaceManager→SandboxBackedFilesystem.requireSandbox), so merging it would leave the failure reported here unfixed — no stand-down is owed on this issue by #3094. - #2833 (
support asynchronous memory flush, stillOPEN/CONFLICTING) does editMemoryFlushMiddleware.java, i.e. the same file a payload-snapshot fix would touch. So "collision-free against all four PRs" needs one qualifier: option (b) is the lowest-overlap shape, but if #2833 lands first the snapshot should be built inside itsasyncFlushpath rather than as a second parallel mechanism.
Summary: the seam question is effectively answered (option b), #3094 does not cover this report, and the only remaining blocker to an automated proposal is an explicit maintainer approval on this thread. This is supplementary context only — the evaluation and its directions are unchanged, and the reply window stays open until 2026-09-15T02:05Z.
Automated evaluation by github-manager-bot
I've opened a fix following option (b) — the direction recorded above as the preferred shape: memory-file IO is routed to a sandbox-independent host-side store whenever the filesystem serving those paths is the per-call sandbox proxy, mirroring the treatment task records received in #2803.
Branch: birdie7761/agentscope-java fix/3110-memory-flush-sandbox-independent — PR incoming right after this comment.
Design decisions worth flagging up front:
- Whole-family routing: flush, consolidation, and the memory tools (
memory_save/memory_get/memory_search) all go through one memory-scoped IO surface, so listing, reads and writes can never split between the sandbox copy and the host store. - User isolation is preserved: the host-side location is the context's runtime-data namespace (
resolveRuntimeDataPath) — the same namespaced layoutRemoteFilesystemapplies to memory paths in distributed deployments. Memory filenames carry no per-user keying, so the directory namespace is their only isolation mechanism. - The consolidation watermark read/write pair is symmetric on the same store (an asymmetric pair would have silently re-merged all historical ledgers on every run).
- No file overlap with #3094 (sandbox-mirror path) — and #3106's quiescence pattern is untouched; this fix composes with both.
Status: 72 h feedback window expired — no automated PR will be generated, this proposal window here is closed
The evaluation above went 72 hours without an /approve, /revise or /reject, so per our workflow the window is closed and no competing PR will be generated. No action is needed from anyone.
That turned out to be the easy case, because the direction recorded in this thread was picked up by the community:
- #3113 (open, non-draft) —
fix(harness): keep memory flush/consolidation IO off the per-call sandbox, by @birdie7761,Fixes #3110, implemented along option (b) (copy the memory payload into a sandbox-independent buffer) as discussed above.
As of this check #3113 is MERGEABLE with reviewDecision=APPROVED and a green check rollup (build ubuntu-latest / build windows-latest, Check License, Check Module Sync, license/cla) at head df3e8e1e5. #3113 stays the active path for this issue and needs a maintainer to merge it, not another implementation.
Thanks @BEN5890 for stating the option preference and @birdie7761 for building it. If maintainers would still like an independent second proposal here, say so on this issue and we will re-evaluate.
Automated by github-manager-bot