Repository navigation
Conversation
…scope-ai#3719) `ReActAgent.Builder.build()` added the middleware and hooks it generates to the builder's own collections and sorted them in place, and `HarnessAgent.Builder.build()` added the harness middlewares to the shared `inner` builder on every build without synchronization. A builder could therefore not be reused: the second agent carried a second copy of every middleware, and concurrent builds threw `ConcurrentModificationException`. A build that failed after auto-wiring also left the state store and the filesystem spec values on the builder. Ports what `main` has: `ReActAgent.Builder.copy()`, with `build()` building from that copy, and `HarnessAgent.Builder.build()` synchronized and building from `inner.copy()`, restoring the builder's write-backs after the build and the auto-wired values after a failed one (agentscope-ai#3811). The effective state store is now pushed to the inner copy on every build, since the shared builder no longer carries it over from an earlier build. Tests: `AgentBuilderReuseTest` and `HarnessBuilderReuseTest` port from `main`, plus a check that a second build waits for a running one and a field-by-field check of `copy()`.
This was referenced Nov 27, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…non-null restore `newInstanceContinuesPersistedConversationAfterPreviousInstanceCloses` relied on the default state store, which lives under `target/`. A run therefore carried the conversation of earlier runs into this one. Once that was long enough (the compaction trigger is 50 messages), the first call compacted it at the price of two more model calls, and the case failed with `expected: <2> but was: <4>`. It builds on an `InMemoryAgentStateStore` of its own now. With an 80-message conversation seeded into the default location, the old case fails and the new one passes. codecov reported one partial line in agentscope-ai#4449 as well, in `RemoteFilesystemSpec.restoreAutoInjectedStore`: the branch for a store that was not null before the failed build. That is the case when an earlier build on the same builder injected the store and a later one fails. A new case builds once, makes the second build fail, and checks that the injected store is still there.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backports to
2.0.xthe part of #3719 that is still open.maingotReActAgent.Builder.copy()in #3441 and the failed-build restore in #3811; nothing here goes beyond them.Problem
On
2.0.xa builder cannot be reused, neither concurrently nor one build after the other:ReActAgent.Builder.build()adds the middleware and hooks it generates to the builder's own collections and sorts them in place. Every build adds another copy, and concurrent builds throwConcurrentModificationExceptionfromArrayList.sort, as reported against 2.0.4.HarnessAgent.Builder.build()adds the harness middlewares to its sharedinnerbuilder on every build and is not synchronized. The second agent built from one builder has 19 middlewares where the first has 10. With 8 concurrent builds, every agent ends up with a chain of 52 or 53 that mixes in instances from other builds.stateStoreOverride, the injected remote store and the sandbox snapshot spec and execution guard on the builder. Replacing the distributed store does not help: the stale local store keeps winning, and the build fails again with the same validation error.Fix
ReActAgent.Builder.copy()returns a builder with the same configuration and fresh registration collections, andbuild()builds from that copy.HarnessAgent.Builder.build()issynchronizedand builds frominner.copy(). The effective state store is now pushed to the inner copy on every build, as onmain. Before, the sharedinnercarried it over from an earlier build; with a copy per build, the second agent would have been built without one.messageBusandasyncToolRegistryare restored after every build, andstateStoreOverridetogether with the auto-wired filesystem spec values after a failed one.RemoteFilesystemSpec.restoreAutoInjectedStoreis added for that, as in fix(harness): restore builder state after failed build #3811.Tests
AgentBuilderReuseTest(core) andHarnessBuilderReuseTest(harness) are ports of themaintests, with two additions that this branch needs:aConcurrentBuildWaitsForTheRunningOneholds one build in the middle ofbuild()and checks that a second one stays out. It is the only test that fails whensynchronizedis dropped, sinceinner.copy()by itself already keeps the chains apart.copyCarriesEveryConfiguredFieldgives every builder field a value that differs from its default and checks the copy field by field.copy()lists the fields by hand, so a field added to2.0.xlater and not copied fails here instead of silently dropping configuration.On unmodified
2.0.xthe core test fails (the second agent has twoDynamicSkillMiddleware), and 7 of the 10 harness cases fail: the sequential and the concurrent reuse, the build that does not wait for a running one, and the four failed-build cases. The other three pass before and after; they pin what the port has to preserve: the state store of a distributed-store wiring, a new instance continuing a persisted conversation, and a store that an earlier build injected staying in place when a later one fails.Each piece of the fix was then broken on its own:
build()withoutcopy()repeatedBuildDoesNotAccumulateGeneratedMiddlewareinnerrepeatedBuildHasFreshRuntimeMiddlewareAndFilesystemBindings,concurrentBuildsKeepIndependentMiddlewareChainssynchronizeddroppedaConcurrentBuildWaitsForTheRunningOnestateStoreOverridenot restored after a failurefailedBuildRestoresAutoWiredRemoteFilesystemStore/failedBuildRestoresAutoWiredSandboxSpecForAnyThrowablerepeatedBuildPreservesDistributedStateStoreWiringand two failed-build casesrepeatedBuildHasFreshRuntimeMiddlewareAndFilesystemBindingscopy()missing a field or a collectioncopyCarriesEveryConfiguredFieldinneronly when auto-wired or absentrepeatedBuildPreservesDistributedStateStoreWiring,builderRemainsReusableAfterFailedBuildaStoreInjectedByAnEarlierBuildIsKeptWhenALaterBuildFailsfailedBuildRestoresAutoWiredRemoteFilesystemStoreThe one that survives is
copy()withoutsynchronized, which no test pins; it is kept as onmain.An earlier version of this description also listed
newInstanceContinuesPersistedConversationAfterPreviousInstanceClosesfor the message-bus mutant. That was wrong: the case failed there because of a flaw in the case itself, not because of the mutant. It built on the default state store, which lives undertarget/, so a run carried the conversation of earlier runs into this one, and once that was long enough the first call compacted it at the price of two more model calls. The case builds on anInMemoryAgentStateStoreof its own now. With an 80-message conversation seeded into the default location, the old case fails withexpected: <2> but was: <4>and the new one passes; with the change, the message-bus mutant leaves the case green.One more case was added where codecov reported an added line without coverage:
aStoreInjectedByAnEarlierBuildIsKeptWhenALaterBuildFailscovers the branch ofRemoteFilesystemSpec.restoreAutoInjectedStorefor a store that was not null before the failed build, which is the case when an earlier build on the same builder injected the store and a later one fails. Every line this PR adds to production code is covered on Linux, branches included.Not in this backport
main-side follow-ups listed under "Suggested fix" in the issue: dropping the serialization and threading the state store through the subagent factories.main: configure the builder before sharing it.Verification
The full
agentscope-coreandagentscope-harnesssuites were run before and after the change on Windows and in a Linux container, at the base as of 2026-11-27. The failing lists are identical; the only difference is the added tests (core +3, harness +10).DangerousPathBypassTest.symlink*(no symlink privilege),SkillBoxTest$UploadContainment.preservesLegacyNamesWithRelativeNonNormalizedWorkDirectorySandbox workspace root must be an absolute path), 2 ×MemoryConsolidatorCommitGateTest,MemoryFlushSandboxIndependenceTest, and the flaky case belowBaseSandboxFilesystemTest.edit_*(python3: not foundin the image),MarketplaceStagerOrphanGcTest.unreadableEntryDoesNotFailStageThe Linux core suite is the one that matters for
copy(): it is the only one of the four that runs without a failure. On the current head (9168a464, with2.0.xmerged in), the container run gives core 3282 run, 0 F + 0 E, and harness 1638 run with the same 3 F + 1 E.CI. The first run of this PR was red for reasons that lay in the base branch: since the backports of 2026-11-13, every PR against
2.0.xfailed the Windows job, at first inagentscope-core, and had its Ubuntu job cancelled. Those causes are fixed by #4454 and #4461, merged into2.0.xon 2026-11-27; the harness part went in with #4461. The current run of this PR is green on both systems, on top of the current2.0.x(core 3282 and harness 1638 tests on Linux, 3277 and 1638 on Windows). The local before/after runs in the table were made before the fixes existed, which is why the "before" column lists failures that the branch no longer has.codecov. Its report of 2026-12-01 on
0bb4080blisted 3 lines ofReActAgent.javawithout coverage, none of them from this change. CI measures coverage on the merge commit with2.0.x, where #4430 had changedReActAgent.javaon 2026-11-30, movingBuilder.copy()37 lines down compared with this branch, while the patch is read from the branch's own head. The last lines of the patch (head 5967–5970) were therefore looked up at 5967–5970 of the merged file, which is the end ofmergeAndSortResults, RAG code this PR does not touch.2.0.xis merged into this branch since9168a464, so both refer to the same file again; the report for that commit shows every modified line covered.One Windows run of this change also failed
HarnessAgentTest.sandboxWithArtifactDeliveryTargetAndDisabledFilesystemTools_promptOmitsDeliverTool. Repeating its scenario 80 times on unmodified2.0.xfails once with the sameSandbox workspace root must be an absolute patherror (0 of 80 with this change): the in-memory sandbox fixture sometimes reports a Windows path as the workspace root. It does not go through the builder.