Skip to content

Implement event handler system for message passing and callbacks#587

Merged
bvdmitri merged 40 commits into
release-6from
callbacks
Mar 20, 2026
Merged

Implement event handler system for message passing and callbacks#587
bvdmitri merged 40 commits into
release-6from
callbacks

Conversation

@bvdmitri
Copy link
Copy Markdown
Member

Summary

  • Introduces a generic event-driven callback system (Event{E}, handle_event, invoke_callback) that lets users hook into every major step of the message passing algorithm: rule calls, message products, form constraint application, and marginal
    computation
  • Adds MessageProductContext to replace bare product strategy arguments, bundling prod_constraint, form_constraint, check strategy, and callbacks into a single context object
  • Refactors variable types with labels, docstrings, and cleaner file organization (variable.jl → generic.jl)

Callback events:

  • BeforeMessageRuleCallEvent / AfterMessageRuleCallEvent
  • BeforeProductOfTwoMessagesEvent / AfterProductOfTwoMessagesEvent
  • BeforeFormConstraintAppliedEvent / AfterFormConstraintAppliedEvent
  • BeforeMarginalComputationEvent / AfterMarginalComputationEvent

Callback handlers can be nothing (no-op), a callable, a Tuple of callables, or a Dict keyed by event name. Includes CallbackResultReducer for aggregating results across events.

Other changes:

  • Removed hardcoded DefaultMessageProdFn / DefaultMarginalProdFn in favor of MessageProductContext
  • Added MethodError hint for mismatched handle_event signatures
  • Added label field to RandomVariable, ConstVariable, DataVariable
  • Improved docstrings for form constraints and variable types
  • Documentation page for callbacks

Test plan

  • New test/callbacks_tests.jl covering all event types, handler variants (nothing, function, tuple, dict), reducers, and merging
  • Extended test/message_tests.jl for MessageProductContext and callback integration in product computations
  • Extended test/variables/random_tests.jl for labels and callback-aware activation
  • Existing tests updated for new API (clusters_tests, dependencies_tests, mixture_tests)

bvdmitri added 30 commits March 5, 2026 15:15
Introduce a new event handler mechanism that allows users to hook into
the message passing procedure via `Event`, `handle_event`, and
`broadcast_event` functions. Events are broadcast before and after
message rule calls, enabling debugging and monitoring capabilities.

Key changes:
- Add `event_handler.jl` with event types and handler interfaces
- Extend `MessageMapping` to include an event handler parameter
- Support both custom handler types and NamedTuple-based handlers
- Add documentation for the events system
- Include comprehensive tests for event handler functionality
this is required to inject the callbacks properly, plus we do the breaking
release already. Could break (and improve) more things as well then
- `compute_product_of_messages` now accepts the `AbstractVariable`,
  makes it easier to identify the variable inside the callback
@bvdmitri bvdmitri added this to RxInfer Mar 20, 2026
@bvdmitri bvdmitri added enhancement New feature or request breaking labels Mar 20, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in RxInfer Mar 20, 2026
@bvdmitri bvdmitri merged commit a40573e into release-6 Mar 20, 2026
0 of 2 checks passed
@bvdmitri bvdmitri deleted the callbacks branch March 20, 2026 14:19
@github-project-automation github-project-automation Bot moved this from Backlog to ✅ Done in RxInfer Mar 20, 2026
bvdmitri added a commit that referenced this pull request Apr 17, 2026
* add Changelog

* Implement event handler system for message passing and callbacks (#587)

* feat: add event handler system for message passing procedure

Introduce a new event handler mechanism that allows users to hook into
the message passing procedure via `Event`, `handle_event`, and
`broadcast_event` functions. Events are broadcast before and after
message rule calls, enabling debugging and monitoring capabilities.

Key changes:
- Add `event_handler.jl` with event types and handler interfaces
- Extend `MessageMapping` to include an event handler parameter
- Support both custom handler types and NamedTuple-based handlers
- Add documentation for the events system
- Include comprehensive tests for event handler functionality

* type in docstring

* inject the event handler in the factor node activation options

* make format

* document the existing events better

* rename event handler to callbacks to better match RxInfer

* update docs

* use make format

* fix typo

* allow to merge callbacks

* allow reduce the result of the callbacks

* add per-event callback reducer

* make format

* remove test method

* start reimplement of the product of two messages

this is required to inject the callbacks properly, plus we do the breaking
release already. Could break (and improve) more things as well then

* fix initial integration with RxInfer.jl

* fix Aqua tests

* caught a small bug in RxInfer tests

* caught another bug from RxInfer.jl tests

* merge stricter formatting

* Refactor the variables, add docstrings, add labels

- `compute_product_of_messages` now accepts the `AbstractVariable`,
  makes it easier to identify the variable inside the callback

* fix documentation build

* 2prev

* add before/after product of two messages callbacks

* fix failing tests

* add before/after product of messages callbacks. add before/after form constraints

* add before/after marginal compute callbacks

* add error hint in case of wrong passed callbacks

* fix test

* temporary fix for the logscale switch rule

* support Dict as callback handler

* fix warnings in the tests

* make format

* refactor to use an abstract Event{E} structure

* add new method for event_name

* use mutable fields instead

* update CHANGELOG

* Trace ID to link before and after events (#589)

* Trace ID to link before and after events

* Add allocations test

* disable some allocation tests on old julia versions

* Change sqrt to + and remove version check

* only check on julia 1.12 again

* Better trace ids (#595)

* create IDs only if callbacks are set

* fix one failing test

* make format

* Refactor addon system to annotations (#596)

* save plan

* update

* refine the plan

* refine plan 2

* implement post_product/post_rule annotations infrastructure

* reimplement the memory addon, remove old implementation

* commit new file

* make format

* update plan

* refactor the message.jl and message_tests.jl

* update marginals.jl and marginal_tests.jl

* remove old bridge functions

* refactor the rule macro and the rule generating expression

* refactor the nodes and tests

* fix remaining places where addons were used

* fix a lot of tests by hand

* fix more tests by hand

* update the plan

* finish the refactor, update the documentation with migration guide,
update CHANGELOG

* fix from RxInfer

* fix bugs caught in RxInfer

* fix documentation build

* add pretty output for input argument records

* 🤖 Auto-format Julia code (#597)

Co-authored-by: bvdmitri <6557701+bvdmitri@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: bvdmitri <6557701+bvdmitri@users.noreply.github.com>

* checkpoint

* checkpoint

* checkpoint

* make format

* fix some tests by hand

* fix more tests by hand

* local update

* fix more tests

* rename vague messagein/messageout

* make format

* major documentation upgrade

* update the CHANGELOG and migration guide

* structural documentation update, add concepts

* improve documentation for existing pages about specific nodes

* update helpers documentation

* add concepts pages

* finish the documentation overhaul

* update the docs

* update the version to 6.0.0

* rename pipeline to stream postprocessors

* update the scheduler to use stream postprocessors instead

* add postprocess marginal stream

* simplify API even further

* update docs

* make format

* fix typo in mixture nodes

* update

* add pre_rule_annotations

* add logo, update README.md

* make logo half the size in README

---------

Co-authored-by: Fons van der Plas <fonsvdplas@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: bvdmitri <6557701+bvdmitri@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking enhancement New feature or request

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

1 participant