Skip to content

Comments

feat: Implement persistent agent state management and process handling#236

Open
doroteaMonaco wants to merge 7 commits intomofa-org:mainfrom
doroteaMonaco:mofa-agent
Open

feat: Implement persistent agent state management and process handling#236
doroteaMonaco wants to merge 7 commits intomofa-org:mainfrom
doroteaMonaco:mofa-agent

Conversation

@doroteaMonaco
Copy link
Contributor

  • Added PersistentAgentRegistry for managing agent metadata on disk.
  • Introduced AgentProcessManager for spawning and managing agent processes.
  • Updated agent commands (start, stop, list) to utilize persistent storage.
  • Enhanced agent metadata structure to include process tracking and state management.
  • Implemented configuration validation for agent startup.
  • Refactored CLI context to include persistent agent registry and process manager.
  • Added timestamp formatting for agent start/stop events.

📋 Summary

This PR introduces the PersistentAgentRegistry and AgentProcessManager subsystems, enabling the CLI to manage agent processes with full lifecycle tracking, persistent state, and cross-platform compatibility. Agent commands now operate on real data stored in ~/.local/share/mofa/agents/ instead of returning hardcoded responses.

🔗 Related Issues

Closes #

Related to #127


🧠 Context

Problem: The agent CLI commands (list, start, stop) were returning hardcoded stub responses without actually managing processes or persisting state.

Solution:

  • New PersistentAgentRegistry saves agent metadata to disk in JSON format
  • New AgentProcessManager spawns/terminates agent processes and tracks PIDs
  • Agent commands updated to read/write persistent state and manage real processes
  • Configuration validation ensures only valid agents can be started

Design Decisions:

  • Using JSON format for persistence (human-readable, easy backup/restore)
  • Cross-platform process management (Unix signals + Windows TerminateProcess)
  • Metadata tracks: PID, state, timestamps, start count for debugging

🛠️ Changes

  • Added PersistentAgentRegistry (src/state/agent_state.rs) - Manages agent metadata lifecycle with disk persistence
  • Added AgentProcessManager (src/utils/process_manager.rs) - Spawns/terminates processes with platform-specific signals
  • Updated agent start command - Validates config, creates process, records metadata
  • Updated agent stop command - Sends SIGTERM/TerminateProcess and updates state
  • Updated agent list command - Reads from disk and displays running/stopped agents with timestamps
  • Enhanced AgentMetadata structure - Added PID, state, timestamps, start count
  • Updated CLI context - Injected PersistentAgentRegistry and AgentProcessManager
  • Added helper: format_timestamp() for human-readable time display (src/commands/agent/list.rs)

🧪 How you Tested

Unit Tests (27 total, all passing):

  1. Created test_agent_metadata_lifecycle - Verifies state transitions (Stopped → Running → Stopped)
  2. Created test_registry_persistence - Ensures agent data survives disk I/O
  3. Created test_list_running_agents - Confirms filtering logic works
  4. Created test_process_manager_creation - Validates PM initialization
  5. Created test_validate_config_missing_file - Error handling for missing config
  6. Created test_validate_config_valid_yaml - Accepts valid YAML config
  7. Created test_validate_config_invalid_yaml - Rejects malformed config

📸 Screenshots / Logs (if applicable)


⚠️ Breaking Changes

  • [X ] No breaking changes
  • Breaking change (describe below)

If breaking:


🧹 Checklist

Code Quality

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

Testing

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

Documentation

  • Public APIs documented
  • README / docs updated (if needed)

PR Hygiene

  • [ X] PR is small and focused (one logical change)
  • [X ] Branch is up to date with main
  • [X ] No unrelated commits
  • [X ] Commit messages explain why, not only what

🚀 Deployment Notes (if applicable)


🧩 Additional Notes for Reviewers

- Added `PersistentAgentRegistry` for managing agent metadata on disk.
- Introduced `AgentProcessManager` for spawning and managing agent processes.
- Updated agent commands (`start`, `stop`, `list`) to utilize persistent storage.
- Enhanced agent metadata structure to include process tracking and state management.
- Implemented configuration validation for agent startup.
- Refactored CLI context to include persistent agent registry and process manager.
- Added timestamp formatting for agent start/stop events.
@lijingrs
Copy link
Collaborator

The code on your branch is not up to date. Please sync and merge the latest code from the main branch.

@doroteaMonaco
Copy link
Contributor Author

Done, thanks!

@lijingrs
Copy link
Collaborator

There are code conflicts. Could you please sync and pull the latest code, then merge it into your current branch in your IDE?

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.

2 participants