Skip to content

Add root mutation detection and shared test helpers#471

Merged
jmalloc merged 9 commits into
mainfrom
root-mutation-checks
Jun 21, 2026
Merged

Add root mutation detection and shared test helpers#471
jmalloc merged 9 commits into
mainfrom
root-mutation-checks

Conversation

@jmalloc

@jmalloc jmalloc commented May 17, 2026

Copy link
Copy Markdown
Member

Add mutation detection for aggregate and process root state within handler scopes. The scope now panics with a descriptive UnexpectedBehavior error when mutations are detected outside of sanctioned operations (e.g., mutating root state between handler method calls).

Changes

  • Mutation detection: Aggregate and process scopes now snapshot root state and detect unsanctioned mutations, reporting the handler identity and source location
  • internal/compare package: Deep equality checks using unsafe reflection, extracted for reuse across scope implementations
  • Shared test helpers (xtesting): ExpectPanicMatching (generic panic recovery with type assertion and match callback) and ExpectLocation (source location assertions)
  • Test cleanup: Eliminated package-local test helpers in aggregate and process packages, replacing them with shared xtesting functions
  • Missing tests: Added mutation detection tests for process scope (before ScheduleDeadline, between two scope calls, at end of handler without scope call)
  • Naming: Renamed test functions and fixtures for clarity (dropped redundant "Scope" prefixes, standardized fixture variable names)

- Add mutation detection for aggregate root state and process root state
  within handler scopes, panicking when mutations are detected outside
  of sanctioned operations
- Extract compare package (internal/compare) for deep equality checks
  using unsafe reflection
- Add shared test helpers to xtesting: ExpectPanicMatching and
  ExpectLocation
- Eliminate package-local test helpers in aggregate and process packages,
  replacing them with shared xtesting functions
- Add missing mutation detection tests for process scope
- Rename test functions and fixtures for clarity

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds root mutation detection for aggregate and process handler scopes, backed by a shared comparison helper and reusable testing assertions.

Changes:

  • Introduces internal/compare with unsafe reflection support for comparing root/message state, including unexported fields and function values.
  • Adds aggregate/process shadow-root checks to detect direct mutation and non-deterministic sanctioned mutations.
  • Refactors tests to use shared xtesting panic/location helpers and adds mutation-detection coverage.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test.go Uses shared comparison helper for fact value matching.
compare.go Delegates default message comparison to shared comparison helper.
fact/aggregate.go Adds snapshot offset metadata to aggregate loaded facts.
internal/compare/doc.go Documents the new comparison package.
internal/compare/compare.go Implements shared deep equality logic.
internal/compare/compare_test.go Adds comparison behavior tests.
internal/compare/internal/unsafereflect/value.go Adds unsafe reflection utilities for unexported fields.
internal/compare/internal/unsafereflect/value_test.go Tests unsafe reflection helper behavior.
internal/compare/internal/unsafereflect/LICENSE.credits Adds attribution for unsafe reflection approach.
internal/x/xtesting/expect.go Adds shared panic and location assertion helpers.
engine/internal/aggregate/controller.go Builds shadow aggregate roots and validates snapshot/event divergence.
engine/internal/aggregate/controller_test.go Updates aggregate controller tests for shared helpers and snapshot behavior.
engine/internal/aggregate/scope.go Adds aggregate mutation detection and non-deterministic ApplyEvent detection.
engine/internal/aggregate/scope_test.go Adds aggregate mutation detection tests.
engine/internal/process/controller.go Builds process shadow roots and checks for end-of-handler direct mutation.
engine/internal/process/controller_test.go Updates process controller tests to shared helpers.
engine/internal/process/scope.go Adds process mutation detection and non-deterministic Mutate detection.
engine/internal/process/scope_test.go Adds process mutation detection tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread engine/internal/aggregate/controller.go Outdated
Comment thread compare.go Outdated
Comment thread internal/compare/compare.go
Comment thread internal/compare/compare.go
jmalloc added 2 commits May 20, 2026 19:47
- Handle invalid (nil) reflect.Value in deepEqual to prevent panic when
  Equal is called with untyped nil arguments
- Add reflect.Array to recursive traversal so arrays containing
  functions use definition-site comparison
- Update DefaultMessageComparator doc comment to describe the actual
  compare.Equal semantics
- Only store aggregate instance in c.instances after events are recorded,
  preventing commands that record no events from leaving behind empty
  instances that change subsequent AggregateInstanceNotFound semantics
- Add ExpectSet to xtesting for unordered slice comparison
- Replace findFact+Expect with ExpectContains in aggregate tests
- Inline expectNoError, expectFacts, and expectEnvelopeSet in process
  tests, removing package-local helper functions

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.

Comment thread engine/internal/process/scope.go
Comment thread engine/internal/aggregate/scope.go
Comment thread internal/compare/compare.go
Comment thread internal/compare/internal/unsafereflect/value.go Outdated
Comment thread internal/compare/internal/unsafereflect/LICENSE.credits Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Comment thread engine/internal/process/controller.go Outdated
Comment thread internal/compare/doc.go
@jmalloc jmalloc force-pushed the root-mutation-checks branch from c160af1 to fce3a86 Compare June 21, 2026 09:58
@jmalloc jmalloc merged commit 8960cf6 into main Jun 21, 2026
9 of 11 checks passed
@jmalloc jmalloc deleted the root-mutation-checks branch June 21, 2026 10:02
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.

2 participants