Skip to content

Conversation

@mikehostetler
Copy link
Contributor

Summary

Addresses the 8 minor code quality issues identified in #103.

Changes

Item 2: Quadratic list concatenation (P4)

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 (P3)

  • Store monitor_ref in waiter struct and use as map key for O(1) lookup
  • Added Process.demonitor/2 with :flush on completion to prevent stale :DOWN messages

Item 4: Trace context gaps (P3)

  • 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 docs mismatch (P2)

  • Changed :jido from required to optional with default of Jido (global instance)
  • Updated documentation and examples to reflect this

Item 7: Lifecycle robustness (P4)

  • handle_call({:attach, ...}), handle_call({:detach, ...}), and handle_cast(:touch, ...) now handle both {:cont, state} and {:stop, reason, state}

Item 8: Persistent term cleanup (P4)

  • Added Jido.Agent.InstanceManager.Cleanup GenServer to clean up persistent_term on supervisor termination

Item 9: Telemetry unit labels (P4)

  • Added to_microseconds/1 helper to convert native time to microseconds
  • All duration_μs log fields now correctly report microseconds

Skipped

Item 6: String.to_atom safety (P3)

Deferred as it requires API/contract decisions about how to handle unknown string keys.

Testing

  • All 1308 tests pass
  • mix quality passes (format, credo, dialyzer)

Closes #103

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 #103 Item 6 - String.to_atom safety

- Remove unsafe safe_to_atom/1 that fell back to String.to_atom for unknown keys
- Remove atomize_string_keys/1 - no longer needed
- Zoi schemas: pass params directly, Zoi with coerce: true handles safely
- NimbleOptions schemas: ActionTool.convert_params_using_schema preserves
  unknown keys as strings (per jido_action PR #56)
- No schema: leave keys as-is to prevent atom exhaustion

Unknown string keys now remain as strings instead of being converted to
atoms, preventing potential DoS attacks from untrusted input.

Co-authored-by: Amp <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-019c0f9a-b9e4-7322-9de7-d96cfe741bf2
@mikehostetler mikehostetler merged commit e40f768 into main Jan 30, 2026
6 checks passed
@mikehostetler mikehostetler deleted the fix/issue-103-code-quality-improvements branch January 30, 2026 16:08
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.

Code quality: 8 minor issues identified during codebase review

2 participants