Skip to content

Bug: Unchecked .unwrap() in Serialization Roundtrips #4

@PrincePundir123

Description

@PrincePundir123

📝 Description

The MoFA kernel exhibits inconsistent error handling patterns for JSON serialization operations. Specifically, documentation and test examples promote the use of .unwrap() on serde_json::to_value(), which creates a severe anti-pattern for users implementing the GraphState trait.

While production code (e.g., JsonState and MessageState) correctly propagates serialization errors, the documentation example encourages users to bypass the AgentError::SerializationError hierarchy entirely. If copied into production, this leads to runtime panics when handling complex nested types, non-serializable structs, or circular references.

⚠️ Impact & Compliance

This is currently a PARTIAL VIOLATION of the MoFA Error Handling Standards (CLAUDE.md, Section I), which mandates a clear error hierarchy unified through the From trait.

  • Current Behavior: Fails with a panic and no context (stack trace only).
  • Expected Behavior: Fails gracefully, propagating AgentError::SerializationError(details) with rich context (key name, underlying serde error).

🛠 Proposed Solution

  1. Update Documentation (state.rs): Replace .unwrap() in the doc examples with proper error propagation using .map_err()/?.
    // Example Fix
    serde_json::to_value(&self.data)
        .map_err(|e| AgentError::SerializationError(e.to_string()))?
  2. Refactor Tests (policy.rs): Remove unsafe .unwrap() patterns in the test suite to expect and handle standard Result types.
  3. Improve Error Context (apply_update): Ensure serialization errors return helpful context (key, type info) rather than generic failures.
  4. Add Validation Tests: Introduce comprehensive tests to verify that non-serializable types and complex nested structures fail gracefully with specific AgentErrors, not panics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions