Skip to content

Add root mutation detection and shared test helpers#471

Open
jmalloc wants to merge 2 commits into
mainfrom
root-mutation-checks
Open

Add root mutation detection and shared test helpers#471
jmalloc wants to merge 2 commits into
mainfrom
root-mutation-checks

Conversation

@jmalloc
Copy link
Copy Markdown
Member

@jmalloc jmalloc commented May 17, 2026

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
Copy link
Copy Markdown

Copilot AI left a comment

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 on lines +196 to +201
if c.instances == nil {
c.instances = map[string]*instance{}
}

inst = &instance{}
c.instances[id] = inst
Comment thread compare.go Outdated
Comment on lines 16 to 17
// It supports comparison of protocol buffers messages using the proto.Equal()
// function. All other types are compared using reflect.DeepEqual().
Comment on lines +63 to +66
case reflect.Struct:
return structEqual(a, b)
default:
return reflect.DeepEqual(a.Interface(), b.Interface())
Comment on lines +32 to +35
return deepEqual(
reflect.ValueOf(a),
reflect.ValueOf(b),
)
- 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
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