Skip to content

fix(core): keep stream event types immutable and validate them - #4312

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
c020627:fix-stream-options-event-types
Nov 23, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
c020627:fix-stream-options-event-types

Conversation

@c020627

@c020627 c020627 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (main @ 2fb5400e)

Description

Problem. StreamOptions is documented as an immutable value object, but it stored the builder's EnumSet by reference and returned that same set from getEventTypes():

this.eventTypes = builder.eventTypes;      // private StreamOptions(Builder)
...
public Set<EventType> getEventTypes() { return eventTypes; }

Two consequences:

  1. Routing can be changed from outside. for (EventType t : options.getEventTypes()) is for-each over EventType.values(), and every per-event decision goes through shouldStream(...), which reads that set. Any consumer that calls options.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.

  2. Argument mistakes surface as JDK-internal failures. Builder.eventTypes(EventType...) delegated to EnumSet.copyOf(Arrays.asList(types)), so the two easy mistakes produce messages that name no option:

    • StreamOptions.builder().eventTypes() → IllegalArgumentException: Collection is empty
    • StreamOptions.builder().eventTypes(EventType.REASONING, null) → NullPointerException: element cannot be null

The 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(...)), or Collections.emptySet() when nothing is selected), and validate eventTypes(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 asserts shouldStream(REASONING) still holds. Fails on the unmodified source (routing lost); passes after the fix.
  • testBuilderRejectsEmptyEventTypes — fails on the unmodified source (Collection is empty does not contain At least one event type); passes after the fix.
  • testBuilderRejectsNullEventType — fails on the unmodified source (NullPointerException is not an IllegalArgumentException); 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:apply then spotless: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 unmodified main @ 2fb5400e on this Windows host, verified by checking out main and 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

  • Code has been formatted with mvn spotless:apply (verified via mvn spotless:check)
  • All tests are passing (mvn test for agentscope-core: no regressions; the 11 symlink failures reproduce on unmodified main on Windows)
  • Javadoc comments are complete and follow project conventions (no new public API; existing javadoc stays accurate)
  • Related documentation has been updated (not needed — no documented behavior changes)
  • Code is ready for review

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.

@zouyx zouyx 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.

LGTM

@jujn
jujn merged commit 88a672a into agentscope-ai:main Nov 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants