File size: 1,536 Bytes
2ac49c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ADR-001: Middleware Architecture Refactor

**Status:** ACCEPTED
**Date:** 2025-12-06
**Decision Makers:** Development Team

---

## Context

The current `src/middleware/` folder is misleadingly named. It contains `SubIterationMiddleware`, which implements a workflow pattern (team→judge loop), not the interceptor middleware pattern used by Microsoft Agent Framework.

Additionally, we're missing proper middleware implementations for:
- Retry logic on transient errors (429, 500, 502, 503, 504)
- Token usage tracking for cost monitoring

---

## Decision

1. **Rename `src/middleware/` to `src/workflows/`** to accurately reflect what it contains
2. **Create new `src/middleware/` with proper MS-pattern middleware:**
   - `RetryMiddleware(ChatMiddleware)` - exponential backoff retry
   - `TokenTrackingMiddleware(ChatMiddleware)` - token usage logging

---

## Consequences

### Positive
- Clearer codebase organization
- Proper use of MS Agent Framework patterns
- HuggingFace 429 crashes will be handled gracefully
- Token usage will be visible for cost monitoring

### Negative
- Requires updating imports in `src/orchestrators/hierarchical.py`
- One-time migration effort

### Neutral
- Aligns with Microsoft Agent Framework conventions

---

## Implementation

See [SPEC-21: Middleware Architecture Refactor](../specs/SPEC-21-MIDDLEWARE-ARCHITECTURE.md) for detailed implementation steps.

---

## References

- Microsoft Agent Framework `_middleware.py`
- [P2 Hardening Issues](../bugs/p2-hardening-issues.md) Issue 2 & 3