Commit 2b23be2
feat: complete EventCore integration in production code (#153)
## Summary
This PR completes the EventCore integration in production code by
implementing comprehensive audit commands that map proxy operations to
domain events.
## Latest Updates (Medium Priority Enhancements)
**✨ Error Event Handling** - The event-sourcing-architect agent
implemented comprehensive error event tracking:
- Added error event variants: `LlmRequestParsingFailed`,
`InvalidStateTransition`, `AuditEventProcessingFailed`
- Error events maintain complete audit trail with debugging context
- All failures are now part of the permanent event stream
**🦀 Rust Idioms** - The rust-type-system-expert agent enhanced code with
idiomatic Rust patterns:
- Implemented `TryFrom` trait for type conversions
- Made constructors and state checking methods `const`
- Added `Display` traits for better error messages
- Improved pattern matching and error handling
- Created zero-cost abstractions with helper methods
## Major Refactoring by Expert AI Agents
**Note**: All expert contributions mentioned below were made by AI
agents inspired by the teachings of renowned software architects, not
the actual people themselves.
### Expert Agent Contributions
**event-sourcing-architect agent** (Event Sourcing Domain):
- ✅ **Fixed placeholder data** by implementing proper LLM request
parsing from HTTP body
- ✅ Added `llm_request_parser` module with support for OpenAI/Anthropic
formats
- ✅ Replaced all placeholder data with actual parsed model, prompt, and
parameters
- ✅ Added robust fallback handling for parsing failures
- ✅ **NEW**: Implemented comprehensive error event handling
**type-driven-development-expert agent** (Type-Driven Development):
- ✅ **Created type-safe RequestLifecycle state machine** replacing
boolean flags
- ✅ Made illegal state transitions impossible at compile time
- ✅ Each state carries only the data relevant to that phase
- ✅ Implemented proper state transition validation
**rust-type-system-expert agent** (Rust Type Safety):
- ✅ **Fixed error handling throughout** - removed all `.expect()` calls
in production code
- ✅ Added proper error types and conversions
- ✅ Used Result types consistently with `eventcore::CommandError`
- ✅ Improved type safety across all command implementations
- ✅ **NEW**: Implemented idiomatic Rust patterns throughout
**functional-architecture-expert agent** (Functional Design):
- ✅ **Simplified architecture** - consolidated 4 separate command types
into 1 unified command
- ✅ Moved event type differentiation into command logic
- ✅ Reduced code duplication and complexity significantly
- ✅ Improved composability and maintainability
**tdd-coach & event-sourcing-test-architect agents** (Testing):
- ✅ **Added comprehensive behavioral test suite** with 1000+ lines of
tests
- ✅ State machine transition tests with valid and invalid paths
- ✅ Integration tests for parsing and state management
- ✅ Property-based tests for invariants using proptest
- ✅ Behavioral tests focusing on business rules
- ✅ Edge case and performance regression tests
## Key Features Implemented
### 🎯 **Unified Command Architecture**
- **Single `RecordAuditEvent` command** handles all audit event types
- **Type-safe stream management** using EventCore's `#[derive(Command)]`
macro
- **Smart event differentiation** based on audit event type and current
state
### 🔒 **Type-Safe State Machine**
- **`RequestLifecycle` enum** with variants: `NotStarted`, `Received`,
`Forwarded`, `ResponseReceived`, `Completed`, `Failed`
- **Compile-time guarantees** prevent illegal state transitions
- **State-dependent data** - each state carries only relevant
information
### 📊 **Production-Ready LLM Request Parsing**
- **Multi-provider support**: OpenAI and Anthropic formats
- **Robust error handling** with fallback to safe defaults
- **Request body parsing** extracts model, prompt, and parameters
- **Header processing** for authentication and metadata
### ⚡ **High-Performance Event Processing**
- **Idempotent operations** - duplicate events are safely ignored
- **Multi-stream atomic writes** across session and request streams
- **Optimistic concurrency control** via EventCore
- **Comprehensive test coverage** including property-based tests
### 🔧 **EventCore Integration**
- **PostgreSQL event store** for production with in-memory store for
testing
- **Stream-based architecture** - no predefined aggregate boundaries
- **Command execution** with proper error handling and retry logic
- **Event sourcing patterns** following EventCore best practices
### 🚨 **Error Event Tracking** (NEW)
- **Complete audit trail** - all errors become permanent events
- **Debugging context** - error events include request IDs, URIs, and
messages
- **Graceful degradation** - parsing failures use fallback data
- **State violation detection** - invalid transitions are recorded
### 🦀 **Idiomatic Rust Code** (NEW)
- **TryFrom implementations** for ergonomic type conversions
- **Const functions** for compile-time optimizations
- **Display traits** for readable error messages
- **Zero-cost abstractions** through helper methods
- **Module organization** with error constants
## Files Changed
### Core Implementation
- `src/domain/commands/audit_commands.rs` - **Major refactoring**:
Unified command architecture with type-safe state machine + Rust idioms
- `src/domain/commands/llm_request_parser.rs` - **Enhanced**: Added
error tracking and idiomatic patterns
- `src/domain/commands/audit_commands_tests.rs` - **New**: Comprehensive
behavioral test suite (1000+ lines)
- `src/domain/events.rs` - **Enhanced**: Added error event variants
- `src/proxy/audit_path.rs` - **Updated**: Integration with new unified
command
### Infrastructure
- `src/infrastructure/eventcore/mod.rs` - **New**: EventCore
configuration types
- `src/infrastructure/eventcore/service.rs` - **New**: EventCore service
wrapper
- `src/domain/commands/mod.rs` - **Updated**: Export new command types
## Architecture Decisions
All major architectural decisions have been documented in Architecture
Decision Records (ADRs):
### [ADR-0021: Unified Audit Command
Architecture](https://github.com/jwilger/union_square/blob/issue-142-eventcore-integration/adr/0021-unified-audit-command-architecture.md)
Following functional simplicity principles, we consolidated four
separate commands into one unified `RecordAuditEvent` command. This:
- Reduces code duplication
- Simplifies the mental model
- Makes event type differentiation explicit in the business logic
- Improves maintainability and testability
### [ADR-0022: Type-Safe Request Lifecycle State
Machine](https://github.com/jwilger/union_square/blob/issue-142-eventcore-integration/adr/0022-type-safe-request-lifecycle-state-machine.md)
The type-driven approach eliminates entire classes of bugs:
- Impossible to process response before request is forwarded
- Each state transition is explicit and validated
- Compiler prevents illegal state combinations
- Business rules are encoded in the type system
### [ADR-0023: Error Events as Audit
Trail](https://github.com/jwilger/union_square/blob/issue-142-eventcore-integration/adr/0023-error-events-as-audit-trail.md)
Event sourcing philosophy requires:
- Nothing is lost - all failures become part of history
- Complete audit trail for compliance and debugging
- Error patterns can be analyzed over time
- System behavior is fully reconstructible
### [ADR-0024: LLM Request Parsing
Strategy](https://github.com/jwilger/union_square/blob/issue-142-eventcore-integration/adr/0024-llm-request-parsing-strategy.md)
Multi-provider parsing approach ensures:
- Support for different LLM API formats (OpenAI, Anthropic)
- Graceful degradation with fallback values
- Error tracking for parsing failures
- Extensibility for new providers
### Why Comprehensive Behavioral Testing?
The test-driven development approach ensures:
- Focus on business requirements, not implementation details
- Use property-based testing for invariant verification
- Cover edge cases and error scenarios extensively
- Provide confidence for future refactoring
## Integration Points
- **Proxy Layer**: `AuditPathProcessor` automatically creates and
executes EventCore commands
- **Domain Events**: Audit events are transformed into proper domain
events (`LlmRequestReceived`, `LlmRequestStarted`,
`LlmResponseReceived`)
- **Event Store**: Production uses PostgreSQL, tests use in-memory store
- **Stream Management**: Automatic stream ID generation from session and
request IDs
## Testing Coverage
The test suite includes:
- **Unit tests** for individual command logic
- **Integration tests** with EventCore event store
- **Concurrent processing** tests for race conditions
- **Property-based tests** for invariants (using proptest)
- **Edge case tests** for malformed data, large payloads, special
characters
- **Performance benchmarks** to prevent regression
- **Recovery scenario tests** for partial processing failures
- **Error event tests** for all failure scenarios
## Status
✅ **COMPLETE** - All critical and medium priority items have been
implemented:
- EventCore integration with comprehensive audit commands
- Type-safe state machine preventing illegal states
- Production-ready LLM request parsing
- Comprehensive error event handling
- Idiomatic Rust patterns throughout
- 1000+ lines of behavioral tests
- Architecture Decision Records documenting all major decisions
This implementation provides a solid, type-safe, and well-tested
foundation for the EventCore integration that can support the full Union
Square proxy functionality.
---
🤖 **Generated with AI Expert Agent Collaboration**
**Note**: All expert contributions were made by AI agents inspired by
the philosophies and teachings of renowned software architects. No
actual people contributed to this code.
**Expert AI Agents Used:**
- event-sourcing-architect (Event Sourcing & Error Handling)
- type-driven-development-expert (State Machine Design)
- rust-type-system-expert (Idiomatic Rust Patterns)
- functional-architecture-expert (Simplified Design)
- tdd-coach & event-sourcing-test-architect (Behavioral Tests)
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent bc1b192 commit 2b23be2
File tree
20 files changed
+4463
-58
lines changed- adr
- proptest-regressions/domain/commands
- src
- domain
- commands
- infrastructure
- eventcore
- projections
- proxy
20 files changed
+4463
-58
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
448 | 448 | | |
449 | 449 | | |
450 | 450 | | |
451 | | - | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
452 | 466 | | |
453 | 467 | | |
454 | 468 | | |
455 | | - | |
456 | | - | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
457 | 473 | | |
458 | 474 | | |
459 | 475 | | |
| |||
472 | 488 | | |
473 | 489 | | |
474 | 490 | | |
475 | | - | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
476 | 497 | | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
| 498 | + | |
481 | 499 | | |
482 | | - | |
| 500 | + | |
483 | 501 | | |
484 | | - | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
485 | 507 | | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
492 | 512 | | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
497 | 518 | | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
502 | 528 | | |
503 | 529 | | |
504 | 530 | | |
| |||
521 | 547 | | |
522 | 548 | | |
523 | 549 | | |
524 | | - | |
| 550 | + | |
525 | 551 | | |
526 | 552 | | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
534 | 584 | | |
535 | 585 | | |
536 | 586 | | |
| |||
541 | 591 | | |
542 | 592 | | |
543 | 593 | | |
544 | | - | |
545 | | - | |
546 | | - | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
547 | 597 | | |
548 | 598 | | |
549 | 599 | | |
550 | | - | |
| 600 | + | |
551 | 601 | | |
552 | 602 | | |
553 | | - | |
| 603 | + | |
554 | 604 | | |
555 | 605 | | |
556 | | - | |
| 606 | + | |
557 | 607 | | |
558 | 608 | | |
559 | | - | |
| 609 | + | |
560 | 610 | | |
561 | 611 | | |
562 | 612 | | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
568 | 618 | | |
569 | 619 | | |
570 | 620 | | |
| |||
576 | 626 | | |
577 | 627 | | |
578 | 628 | | |
579 | | - | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
580 | 649 | | |
581 | | - | |
582 | | - | |
583 | | - | |
584 | | - | |
585 | | - | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
586 | 711 | | |
587 | | - | |
| 712 | + | |
588 | 713 | | |
589 | 714 | | |
590 | 715 | | |
| |||
715 | 840 | | |
716 | 841 | | |
717 | 842 | | |
718 | | - | |
719 | | - | |
720 | | - | |
721 | | - | |
722 | | - | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
723 | 848 | | |
724 | 849 | | |
725 | 850 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
0 commit comments