Repository navigation
fix(core): keep stream event types immutable and validate them - #4312
Merged
Merged
Conversation
StreamOptions stored the builder's EnumSet directly and returned it from getEventTypes(), so any consumer holding the options could clear or add entries and silently change which events every other consumer of the same options receives. Builder.eventTypes(EventType...) also delegated to EnumSet.copyOf(Arrays.asList(types)), which surfaces JDK-internal failures for the two easy mistakes: zero arguments throws 'IllegalArgumentException: Collection is empty' and a null element throws 'NullPointerException: element cannot be null', neither naming the offending option. Expose an unmodifiable copy and validate the arguments, rejecting an empty selection and null elements with messages that name the option.
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.
AgentScope-Java Version
2.1.0-SNAPSHOT (
main@2fb5400e)Description
Problem.
StreamOptionsis documented as an immutable value object, but it stored the builder'sEnumSetby reference and returned that same set fromgetEventTypes():Two consequences:
Routing can be changed from outside.
for (EventType t : options.getEventTypes())is for-each overEventType.values(), and every per-event decision goes throughshouldStream(...), which reads that set. Any consumer that callsoptions.getEventTypes().clear()— or adds an entry — silently changes which events every other consumer of the same options object receives, with no error and no log.Argument mistakes surface as JDK-internal failures.
Builder.eventTypes(EventType...)delegated toEnumSet.copyOf(Arrays.asList(types)), so the two easy mistakes produce messages that name no option:StreamOptions.builder().eventTypes()→IllegalArgumentException: Collection is emptyStreamOptions.builder().eventTypes(EventType.REASONING, null)→NullPointerException: element cannot be nullThe empty case is the more likely one in practice, since an empty varargs array is what a caller ends up with when the selection is computed and comes back empty.
Change. Expose an unmodifiable copy (
Collections.unmodifiableSet(EnumSet.copyOf(...)), orCollections.emptySet()when nothing is selected), and validateeventTypes(EventType...)eagerly, rejecting an empty selection and null elements with messages that name the option. Callers that only read the set are unaffected; no behavior change for any valid input; no new dependencies; no public API signature changes.Regression evidence. Three new tests in
StreamOptionsTest:testReturnedEventTypesCannotChangeRouting— clears the returned set and assertsshouldStream(REASONING)still holds. Fails on the unmodified source (routing lost); passes after the fix.testBuilderRejectsEmptyEventTypes— fails on the unmodified source (Collection is emptydoes not containAt least one event type); passes after the fix.testBuilderRejectsNullEventType— fails on the unmodified source (NullPointerExceptionis not anIllegalArgumentException); passes after the fix.Verification (module
agentscope-core):mvn -q -pl agentscope-core -Dtest=StreamOptionsTest test— before fix: 3 failed / 17 passed; after fix: 20 passed.mvn -q -pl agentscope-core spotless:applythenspotless:check— passed.mvn -q -pl agentscope-core test—Tests run: 3505, Failures: 11, Errors: 0, Skipped: 9. All 11 failures are symlink tests (JsonFileAgentStateStoreTest.sessionOperationsShareLocksThroughRootSymlink×8,DangerousPathBypassTest×2,PathSandboxInterceptorTest.escapeAttemptsAreDenied) that fail identically on unmodifiedmain@2fb5400eon this Windows host, verified by checking outmainand re-running those three classes (45 tests, same 11 failures).Scope is confined to one source file and its existing test file; no open PR touches either.
Checklist
mvn spotless:apply(verified viamvn spotless:check)mvn testforagentscope-core: no regressions; the 11 symlink failures reproduce on unmodifiedmainon Windows)