Skip to content

Conversation

@mikehostetler
Copy link
Contributor

Summary

Fixes #102 - Sensor.Runtime doesn't accept documented handle_event/2 return shapes.

Solution

Instead of adding complexity to support multiple return formats, simplified the API to use a directive-only format that matches init/2:

Before (confusing)

  • {:ok, state, signals} - signals as bare structs
  • {:ok, state, signals, directives} - signals and directives separate
  • {:ok, state, directives} - with signals wrapped as {:emit, signal}

After (unified)

  • {:ok, state} - no directives
  • {:ok, state, directives} - directives include {:emit, signal}, {:schedule, ms}, etc.

Changes

  • lib/jido/sensor.ex: Updated @callback handle_event/2 typespec and docs
  • lib/jido/sensor/runtime.ex: Added Code.ensure_loaded? to fix module loading; simplified handler
  • lib/jido/sensors/heartbeat_sensor.ex: Updated to use {:emit, signal} directive format
  • test/jido/sensor/runtime_test.exs: Added Heartbeat integration test

Benefits

  • Consistent API: init/2 and handle_event/2 use identical return formats
  • Simpler runtime: no detection logic needed
  • Clearer mental model: everything is a directive

mikehostetler and others added 2 commits January 30, 2026 09:37
Fixes #103

## Changes

### Item 2: Quadratic list concatenation
- Changed acc ++ new patterns to prepend+reverse in:
  - lib/jido/agent/strategy/direct.ex
  - lib/jido/agent/strategy/fsm.ex
  - lib/jido/agent/state_ops.ex

### Item 3: Completion waiter monitor leak
- Store monitor_ref in waiter struct and use as map key
- O(1) map lookup on :DOWN instead of O(n) scan
- Added Process.demonitor/2 with :flush on completion

### Item 4: Trace context gaps
- Added TraceContext.ensure_from_signal/1 to handle_info({:scheduled_signal, ...})
- Added TraceContext.ensure_from_signal/1 to handle_info({:signal, ...})

### Item 5: :jido option defaults
- Changed :jido from required to optional with default of Jido (global instance)
- Updated documentation and examples

### Item 7: Lifecycle robustness
- handle_call({:attach, ...}), handle_call({:detach, ...}), and handle_cast(:touch, ...)
  now handle both {:cont, state} and {:stop, reason, state}

### Item 8: Persistent term cleanup
- Added Jido.Agent.InstanceManager.Cleanup GenServer to clean up
  persistent_term on supervisor termination

### Item 9: Telemetry unit labels
- Added to_microseconds/1 helper to convert native time to microseconds
- All duration_μs log fields now correctly report microseconds

Amp-Thread-ID: https://ampcode.com/threads/T-019c0f7f-f4c2-7615-bcea-3c8d341d438f
Co-authored-by: Amp <[email protected]>
Fixes #102 - Sensor.Runtime now correctly handles handle_event/2 returns.

Changes:
- Updated @callback handle_event/2 to return {:ok, state} or {:ok, state, directives}
- Removed 4-tuple format {:ok, state, signals, directives} in favor of unified directive list
- Signals are now emitted via {:emit, signal} directive, matching init/2 pattern
- Added Code.ensure_loaded? to fix module loading race condition
- Updated Jido.Sensors.Heartbeat to use new format
- Added integration test for Heartbeat sensor

Amp-Thread-ID: https://ampcode.com/threads/T-019c0f8e-6d30-730c-96d4-74527a19a17f
Co-authored-by: Amp <[email protected]>
@mikehostetler mikehostetler merged commit 86f7d3b into main Jan 30, 2026
6 checks passed
@mikehostetler mikehostetler deleted the fix/sensor-runtime-handle-event-102 branch January 30, 2026 16:09
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.

Bug: Sensor.Runtime doesn't accept documented handle_event/2 return shapes

2 participants