Skip to content

Comments

feat: implement Reflection agentic design pattern#231

Merged
lijingrs merged 2 commits intomofa-org:mainfrom
indrasn0wal:feat/reflection-agent
Feb 23, 2026
Merged

feat: implement Reflection agentic design pattern#231
lijingrs merged 2 commits intomofa-org:mainfrom
indrasn0wal:feat/reflection-agent

Conversation

@indrasn0wal
Copy link
Contributor

📋 Summary

Implements the Reflection agentic design pattern (Chapter 4 of Agentic Design Patterns) in MoFA. The pattern runs a generate → critique → refine loop where an agent produces a draft, critiques it, and iteratively improves it — addressing Open Task 8.

🔗 Related Issues

Closes #179


🧠 Context

The Reflection pattern was missing from MoFA's agentic pattern library. While ThoughtStepType::Reflection and ThoughtStep::reflection() already existed in mofa-kernel, there was no implementation of the actual critique–refine loop. This PR fills that gap by implementing the full pattern in mofa-foundation and wiring it into the existing react module and SDK.


🛠️ Changes

  • New reflection.rs in mofa-foundation/src/react/ — types, config, agent, builder, and run loop
  • Updated react/mod.rs — registered module, exported types, added to prelude
  • New examples/reflection_agent/ — end-to-end example working with any LLM provider
  • Updated examples/Cargo.toml — added reflection_agent to workspace members

🧪 How you Tested

  1. cargo test -p mofa-foundation reflection — 3 unit tests pass
  2. cd examples/reflection_agent && cargo run — tested end-to-end with OpenAI GPT-4, ran 3 rounds of generate → critique → refine successfully

📸 Screenshots / Logs (if applicable)

(mofa-dev) indrasn0wal@Indras-MacBook-Air mofa % cargo test -p mofa-foundation reflection
   Compiling mofa-foundation v0.1.0 (/Users/indrasn0wal/Desktop/mofa/crates/mofa-foundation)
warning: cannot test inner items
   --> crates/mofa-foundation/src/collaboration/types.rs:768:9
    |
768 |         #[tokio::test]
    |         ^^^^^^^^^^^^^^
    |
    = note: #[warn(unnameable_test_items)] on by default
warning: mofa-foundation (lib test) generated 1 warning
    Finished test profile [unoptimized + debuginfo] target(s) in 9.24s
     Running unittests src/lib.rs (target/debug/deps/mofa_foundation-f1dec615a9df58fa)
running 3 tests
test react::reflection::tests::config_builder ... ok
test react::reflection::tests::config_defaults ... ok
test react::reflection::tests::reflection_step_serialization ... ok
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 183 filtered out; finished in 0.00s
2026-02-22T10:39:25.899747Z  INFO reflection_agent: ==========================================
2026-02-22T10:39:25.899843Z  INFO reflection_agent:   MoFA Reflection Agent Example
2026-02-22T10:39:25.899852Z  INFO reflection_agent: ==========================================
2026-02-22T10:39:25.929416Z  INFO reflection_agent: Task: Explain the concept of ownership in Rust and why it matters.

2026-02-22T10:39:33.597491Z  INFO mofa_foundation::react::reflection: [Reflection] Round 1 - critiquing draft (len=1544)
2026-02-22T10:39:40.030239Z  INFO mofa_foundation::react::reflection: [Reflection] Round 1 - refining
2026-02-22T10:39:52.784465Z  INFO mofa_foundation::react::reflection: [Reflection] Round 2 - critiquing draft (len=1927)
2026-02-22T10:39:58.277431Z  INFO mofa_foundation::react::reflection: [Reflection] Round 2 - refining
2026-02-22T10:40:08.352702Z  INFO mofa_foundation::react::reflection: [Reflection] Round 3 - critiquing draft (len=2259)
2026-02-22T10:40:15.804855Z  INFO mofa_foundation::react::reflection: [Reflection] Completed in 3 rounds, 49875ms

⚠️ Breaking Changes

  • No breaking changes

🧹 Checklist

Code Quality

  • Code follows Rust idioms and project conventions
  • cargo fmt run
  • cargo clippy passes without warnings

Testing

  • Tests added/updated
  • cargo test passes locally without any error

Documentation

  • Public APIs documented

PR Hygiene

  • PR is small and focused (one logical change)
  • No unrelated commits

🧩 Additional Notes for Reviewers

  • Critic defaults to same agent as generator — separate critic supported via .with_critic(Arc<LLMAgent>)
  • Early stopping implemented — loop exits when critic says "no major improvements needed"
  • Uses existing ThoughtStep::reflection() from kernel for trace consistency
  • Mock LLM tests for the full run loop are left as a follow-up

@indrasn0wal
Copy link
Contributor Author

@BH3GEI Please review it.

@lijingrs lijingrs merged commit 3bd2723 into mofa-org:main Feb 23, 2026
5 checks passed
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.

feat: implement Reflection agentic design pattern

2 participants