Skip to main content

Redis-backed mailbox utilities from Beast Mode

Project description

Beast Mailbox Core

PyPI version Python Versions Downloads PyPI status Wheel License: MIT Code style: black Quality Gate Status Coverage SonarQube Cloud Tests Documentation Maintainability Rating

Redis-backed mailbox utilities extracted from Beast Mode. Enterprise-grade with 90% coverage, 52% documentation density, and ZERO defects!

Features

  • Durable messaging via Redis streams (XADD/XREADGROUP)
  • Consumer groups per agent ID
  • Async handler registration for inbound messages
  • Simple CLI entry points (beast-mailbox-service, beast-mailbox-send)
  • One-shot message inspection with optional acknowledge/trim operations

Installation

pip install beast-mailbox-core

Quickstart

Redis Configuration

Priority order:

  1. CLI flags (highest priority - explicit override)
  2. REDIS_URL environment variable (convenient default)
  3. Hardcoded defaults (localhost:6379)

Using REDIS_URL (recommended for OpenFlow Playground):

export REDIS_URL="redis://:password@host:port/db"
beast-mailbox-service my-agent --echo  # Just works!

Using Individual Environment Variables (Programmatic Usage):

export REDIS_HOST="prod-redis.example.com"
export REDIS_PORT="6379"
export REDIS_PASSWORD="secret"
export REDIS_DB="0"

Python API automatically reads environment variables:

from beast_mailbox_core import RedisMailboxService

# Automatically reads from REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, REDIS_DB
# Falls back to REDIS_URL if REDIS_HOST not set
# Only defaults to localhost:6379 if no env vars are set
service = RedisMailboxService("my-agent", config=None)  # Reads from env!

Using CLI flags:

beast-mailbox-service my-agent \
  --redis-host 192.168.1.119 \
  --redis-password beastmode2025 \
  --redis-port 6379 \
  --redis-db 0

CLI flags override environment variables when both are provided.

Start a streaming listener

# With REDIS_URL set (easiest)
export REDIS_URL="redis://:beastmode2025@192.168.1.119:6379/0"
beast-mailbox-service herbert --echo

# Or with CLI flags
beast-mailbox-service herbert \
  --redis-host 192.168.1.119 --redis-password beastmode2025 --echo

Send messages

# With REDIS_URL set
export REDIS_URL="redis://:beastmode2025@192.168.1.119:6379/0"
beast-mailbox-send devbox herbert --message "ping"

# Or with CLI flags
beast-mailbox-send devbox herbert --message "ping" \
  --redis-host 192.168.1.119 --redis-password beastmode2025

# Send structured JSON payload
beast-mailbox-send devbox herbert --json '{"task": "sync", "priority": "high"}' \
  --message-type task_update

One-Shot Message Inspection

Read-only inspection (default)

# View the latest message without consuming it
beast-mailbox-service devbox --latest --count 1 \
  --redis-host vonnegut --redis-password beastmode2025

# View the 5 most recent messages
beast-mailbox-service devbox --latest --count 5 \
  --redis-host vonnegut --redis-password beastmode2025 --verbose

Acknowledge messages (semi-destructive)

⚠️ Warning: Marks messages as acknowledged in the consumer group, preventing redelivery.

# View and acknowledge the latest 5 messages
beast-mailbox-service devbox --latest --count 5 --ack \
  --redis-host vonnegut --redis-password beastmode2025

Output:

📬 devbox <- alice (direct_message) [1234567890-0]: {'text': 'Hello'}
✓ Acknowledged 5 message(s) in group devbox:group

Trim messages (destructive)

⚠️ Warning: Permanently deletes messages from the stream. Cannot be undone.

# View and delete the latest 5 messages
beast-mailbox-service devbox --latest --count 5 --trim \
  --redis-host vonnegut --redis-password beastmode2025

Output:

📬 devbox <- alice (direct_message) [1234567890-0]: {'text': 'Hello'}
🗑️  Deleted 5 message(s) from stream

Combined acknowledge and trim (common cleanup pattern)

# View, acknowledge, and delete messages in one operation
beast-mailbox-service devbox --latest --count 10 --ack --trim \
  --redis-host vonnegut --redis-password beastmode2025 --verbose

CLI Options Reference

beast-mailbox-service <agent_id> [options]

Positional Arguments:

  • agent_id - Unique identifier for this mailbox (e.g., devbox, poe, herbert)

Connection Options:

  • --redis-host HOST - Redis server host (default: localhost)
  • --redis-port PORT - Redis server port (default: 6379)
  • --redis-password PASSWORD - Redis authentication password
  • --redis-db DB - Redis database index (default: 0)

Operation Modes:

  • --latest - One-shot mode: fetch latest messages and exit (default: streaming mode)
  • --count N - Number of messages to fetch in one-shot mode (default: 1)

Destructive Operations (require --latest):

  • --ack - Acknowledge messages after displaying them
  • --trim - Delete messages after displaying them (implies acknowledgement)

Other Options:

  • --poll-interval SECONDS - Seconds between stream polls in streaming mode (default: 2.0)
  • --verbose - Enable debug logging
  • --echo - (deprecated, use --verbose)

beast-mailbox-send <sender> <recipient> [options]

Positional Arguments:

  • sender - Agent ID sending the message
  • recipient - Agent ID receiving the message

Message Options:

  • --message TEXT - Plain text message
  • --json JSON - JSON payload
  • --message-type TYPE - Message type classification (default: direct_message)

Connection Options: (same as beast-mailbox-service)

Best Practices

Safety Guidelines

  1. Always start with read-only inspection:

    beast-mailbox-service myagent --latest --count 5
    
  2. Use --count to limit destructive operations: Don't accidentally delete hundreds of messages - specify a reasonable count.

  3. Enable --verbose for audit trails: See exactly what was acknowledged/deleted with timestamps and message IDs.

  4. --ack is safer than --trim: Acknowledgement prevents redelivery but keeps messages in the stream for debugging.

  5. Back up before trimming in production: Use redis-cli DUMP beast:mailbox:<agent>:in to export the stream first.

  6. Check exit codes in automation:

    • Exit code 0 = success
    • Non-zero = failure (check stderr for details)

Error Handling

  • Partial failures (e.g., network interruption) are reported clearly
  • Acknowledgement failures prevent trimming to avoid data loss
  • Consumer group creation errors are handled gracefully (BUSYGROUP)

Environment Notes

This package disables the heavy observability hooks by default. You still need a Redis instance accessible to all nodes. If you run alongside the full Beast Mode stack, simply point to the same Redis host.

Set BEAST_MODE_PROMETHEUS_ENABLED=false to explicitly disable metrics collection.

Troubleshooting

Messages not appearing:

  • Verify Redis connection with redis-cli -h <host> -a <password> ping
  • Check the stream exists: redis-cli -h <host> XLEN beast:mailbox:<agent>:in
  • Ensure agent IDs match (sender → recipient)

Consumer group errors:

  • "BUSYGROUP" errors are normal and handled automatically
  • Group names are <agent_id>:group format

REDIS_URL parsing errors:

  • Verify REDIS_URL format: redis://:password@host:port/db
  • Check for URL encoding issues (special characters in password/host)
  • Use --redis-host and other CLI flags to override if REDIS_URL is misconfigured
  • Test REDIS_URL parsing: python -c "from beast_mailbox_core.cli import parse_redis_url; print(parse_redis_url('redis://:pass@host:6379/0'))"

Blocking in tests:

  • See .kiro/steering/testing-patterns.md for guidance on mocking ReflectiveModule

Documentation

  • 📘 API Reference - Comprehensive API documentation for integration
  • 🤖 beast-agent Integration - Authoritative guide for using beast-agent with beast-mailbox-core ⭐ (v0.1.3+)
  • 📖 Usage Guide - Detailed usage patterns and examples

Note: Deprecated temporary guides (see beast-agent repo for authoritative docs):

For AI Maintainers

This repository was built 100% by AI agents and is maintained by AI agents.

If you're an AI agent tasked with maintaining this repository, start here:

These documents contain critical context, quality standards, testing requirements, release procedures, and lessons learned from building this project from crisis to excellence.

Version History

0.2.0 (2025-10-10)

  • Added --ack flag for acknowledging messages after inspection
  • Added --trim flag for deleting messages from the stream
  • Comprehensive test suite (21 tests, all passing)
  • Enhanced error handling for partial failures
  • Clear logging with emoji indicators

0.1.0 (Initial release)

  • Basic streaming mailbox service
  • One-shot message inspection
  • Message sending utility

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

beast_mailbox_core-0.4.4.tar.gz (37.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

beast_mailbox_core-0.4.4-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file beast_mailbox_core-0.4.4.tar.gz.

File metadata

  • Download URL: beast_mailbox_core-0.4.4.tar.gz
  • Upload date:
  • Size: 37.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for beast_mailbox_core-0.4.4.tar.gz
Algorithm Hash digest
SHA256 12ed5921d7cc6ec58bc8e0c72a11c4dd6e26dd35f1f4b1409002b9dd474eb558
MD5 71d36627056315afecc7679e6c981b87
BLAKE2b-256 8736cda5de0a0a96e9dc3ebcbb215fb5be29097923bb06e91d2c80e3baeb319b

See more details on using hashes here.

File details

Details for the file beast_mailbox_core-0.4.4-py3-none-any.whl.

File metadata

File hashes

Hashes for beast_mailbox_core-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3b39c99b40fcd27f65e82845a9711bb94c241bb5cf916c60fcaa0fc45ee35196
MD5 8d0bf1dc2e7b02451cff5ac4a578c7a8
BLAKE2b-256 086c00afa2b5c4c37d16b4a9cc58d65fbbabcc72bb5c47f057442c29e8353ee5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page