trusty-mpm 1.0.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
Documentation
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
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
711
712
713
714
715
716
717
718
719
# Completeness Anti-Patterns

Anti-patterns related to incomplete testing practices: partial mocks that hide structural dependencies, and treating tests as optional afterthoughts instead of integral parts of implementation.

## Anti-Pattern 4: Incomplete Mocks

### The Violation

**[TypeScript/Jest]**
```typescript
// ❌ BAD: Partial mock - only fields you think you need
const mockResponse = {
  status: 'success',
  data: { userId: '123', name: 'Alice' }
  // Missing: metadata that downstream code uses
};

// Later: breaks when code accesses response.metadata.requestId
```

**[Python/pytest]**
```python
# ❌ BAD: Incomplete mock missing fields
from unittest.mock import Mock

def test_display_user_profile():
    mock_user = Mock()
    mock_user.id = 123
    mock_user.name = "Alice"
    # Missing: email, profile, metadata

    result = display_user_profile(mock_user)

    # Test passes but production code may access user.profile.timezone
    # → AttributeError in production!
```

**Why this is wrong:**
- **Partial mocks hide structural assumptions** - You only mocked fields you know about
- **Downstream code may depend on fields you didn't include** - Silent failures
- **Tests pass but integration fails** - Mock incomplete, real API complete
- **False confidence** - Test proves nothing about real behavior
- **Breaking changes go undetected** - API changes don't fail tests

**The Iron Rule:** Mock the COMPLETE data structure as it exists in reality, not just fields your immediate test uses.

**Real-world impact:**

**[TypeScript/Jest]**
```typescript
// Your test (incomplete mock)
const mockUser = { id: '123', name: 'Alice' };
// Passes ✅

// Production code elsewhere
function getUserTimezone(user) {
  return user.profile.timezone;  // undefined - breaks in production!
}
```

**[Python/pytest]**
```python
# Your test (incomplete mock)
mock_user = Mock(id=123, name="Alice")
# Passes ✅

# Production code elsewhere
def get_user_timezone(user):
    return user.profile.timezone  # AttributeError in production!
```

### Complete Mock Strategy

**The fix:**

**[TypeScript/Jest]**
```typescript
// ✅ GOOD: Mirror real API completeness
const mockResponse = {
  status: 'success',
  data: {
    userId: '123',
    name: 'Alice',
    email: 'alice@example.com',
    profile: {
      timezone: 'America/New_York',
      locale: 'en-US',
      avatar: 'https://...'
    }
  },
  metadata: {
    requestId: 'req-789',
    timestamp: 1234567890,
    version: 'v2'
  }
  // All fields real API returns
};
```

**[Python/pytest]**
```python
# ✅ GOOD: Complete mock or use fixture
from dataclasses import dataclass
from typing import Optional

@dataclass
class User:
    id: int
    name: str
    email: str
    profile: 'UserProfile'
    metadata: dict

@dataclass
class UserProfile:
    timezone: str
    locale: str
    avatar: Optional[str]

# Fixture factory
def create_test_user(**overrides) -> User:
    """Complete user with all required fields"""
    defaults = {
        "id": 123,
        "name": "Alice",
        "email": "alice@example.com",
        "profile": UserProfile(
            timezone="UTC",
            locale="en-US",
            avatar=None
        ),
        "metadata": {}
    }
    return User(**{**defaults, **overrides})

def test_display_user_profile():
    user = create_test_user(name="Alice")

    result = display_user_profile(user)

    assert "Alice" in result
    assert "UTC" in result
```

### How to Create Complete Mocks

**Step 1: Reference real API documentation**
```typescript
// ❌ BAD - mocking from memory
const mock = { id: 1, name: 'test' };

// ✅ GOOD - reference API docs
// From API docs: GET /api/users/:id returns:
// {
//   id: number,
//   name: string,
//   email: string,
//   createdAt: ISO8601,
//   profile: { ... },
//   permissions: string[]
// }

const mock = {
  id: 1,
  name: 'test',
  email: 'test@example.com',
  createdAt: '2024-01-01T00:00:00Z',
  profile: { /* complete profile */ },
  permissions: ['read', 'write']
};
```

**Step 2: Use TypeScript types**
```typescript
// ✅ TypeScript ensures completeness
interface User {
  id: string;
  name: string;
  email: string;
  profile: UserProfile;
  metadata: UserMetadata;
}

// TypeScript error if incomplete
const mockUser: User = {
  id: '123',
  name: 'Alice'
  // Error: Missing email, profile, metadata
};

// Must be complete
const mockUser: User = {
  id: '123',
  name: 'Alice',
  email: 'alice@example.com',
  profile: { timezone: 'UTC', locale: 'en' },
  metadata: { createdAt: '2024-01-01T00:00:00Z' }
};
```

**Step 3: Extract mock factories**
```typescript
// ✅ Reusable complete mocks
// test-utils/factories/user.ts
export function createMockUser(overrides?: Partial<User>): User {
  return {
    id: '123',
    name: 'Test User',
    email: 'test@example.com',
    profile: {
      timezone: 'UTC',
      locale: 'en-US',
      avatar: null
    },
    metadata: {
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-01T00:00:00Z',
      version: 1
    },
    permissions: ['read'],
    ...overrides  // Allow customization
  };
}

// Usage
const user = createMockUser({ name: 'Alice' });
// All fields present, name overridden
```

### Detecting Incomplete Mocks

**Red flags:**
- Creating mocks without referencing API docs
- TypeScript `any` or `Partial<T>` for mocks
- Different tests have different mock shapes for same type
- Mock has fewer fields than real API
- Comments like "add fields as needed"

**Gate Function:**

```
BEFORE creating mock responses:
  Check: "What fields does the real API response contain?"

  Actions:
    1. Open API documentation or OpenAPI spec
    2. List ALL fields in response
    3. Include ALL fields in mock (use TypeScript types)
    4. Verify mock matches real response schema completely

  Critical:
    If you're creating a mock, you must understand the ENTIRE structure
    Partial mocks fail silently when code depends on omitted fields

  If uncertain:
    - Include all documented fields
    - Use TypeScript interfaces to enforce completeness
    - Create factory functions for reusable complete mocks

  Red flags:
    - "I'll add more fields later"
    - "Just mocking what I need"
    - Creating mock from memory
    - Different mock shapes for same entity
```

### Common Incomplete Mock Scenarios

**Scenario 1: Nested objects**
```typescript
// ❌ BAD - incomplete nesting
const mockUser = {
  id: '123',
  profile: { name: 'Alice' }  // Missing profile.timezone, profile.locale
};

// ✅ GOOD - complete nesting
const mockUser = {
  id: '123',
  profile: {
    name: 'Alice',
    timezone: 'UTC',
    locale: 'en-US',
    avatar: null,
    bio: null
  }
};
```

**Scenario 2: Arrays**
```typescript
// ❌ BAD - empty array when real data has items
const mockResponse = {
  users: []  // Real API always has pagination metadata
};

// ✅ GOOD - realistic array with metadata
const mockResponse = {
  users: [
    { id: '1', name: 'Alice', /* complete user */ },
    { id: '2', name: 'Bob', /* complete user */ }
  ],
  pagination: {
    total: 2,
    page: 1,
    perPage: 10,
    hasNext: false
  }
};
```

**Scenario 3: Optional fields**
```typescript
// ❌ BAD - omitting optional fields
const mockUser = {
  id: '123',
  name: 'Alice'
  // Omitted optional fields - downstream code may check them
};

// ✅ GOOD - include optional fields (as null/undefined)
const mockUser = {
  id: '123',
  name: 'Alice',
  bio: null,           // Optional field explicitly null
  website: undefined,  // Optional field explicitly undefined
  avatar: null
};
```

### Mock Completeness Levels

**Level 1: Minimal (AVOID)**
```typescript
// Only immediate test needs
const mock = { id: '123' };
// Risk: High - downstream code breaks
```

**Level 2: Partial (RISKY)**
```typescript
// Common fields included
const mock = { id: '123', name: 'Alice', email: 'alice@example.com' };
// Risk: Medium - missing nested/optional fields
```

**Level 3: Complete (REQUIRED)**
```typescript
// All fields from API spec
const mock = {
  id: '123',
  name: 'Alice',
  email: 'alice@example.com',
  profile: { /* complete */ },
  metadata: { /* complete */ },
  permissions: ['read']
};
// Risk: Low - matches real API
```

**Level 4: Factory-based (BEST)**
```typescript
// Reusable factory with defaults
const mock = createMockUser({ name: 'Alice' });
// Risk: Minimal - centralized, typed, complete
```

## Anti-Pattern 5: Tests as Afterthought

### The Violation

**[TypeScript/Jest]**
```typescript
// ❌ BAD - Implementation before tests
// 1. Wrote UserService class
// 2. Wrote createUser() method
// 3. Wrote updateUser() method
// 4. Now writing tests to achieve coverage

test('creates user', () => {
  // Test confirms what code DOES, not what it SHOULD do
  // Bugs baked into test expectations
});
```

**[Python/pytest]**
```python
# ❌ BAD - Implementation before tests
# 1. Wrote create_user() function
# 2. Wrote update_user() function
# 3. Wrote delete_user() function
# 4. Now writing tests to achieve coverage

def test_create_user():
    # Test confirms what code DOES, not what it SHOULD do
    # Bugs baked into test expectations
    pass
```

**Why this is wrong:**
- Testing is part of implementation, not optional follow-up
- TDD would have caught this
- Can't claim complete without tests
- Tests written after code are biased toward existing implementation
- Missing tests = incomplete feature
- Technical debt from day one

### The Fix - TDD

**[TypeScript/Jest]**
```typescript
// ✅ GOOD - Test first (TDD)

// Step 1: RED - Write failing test
test('creates user with valid email', () => {
  const service = new UserService();
  expect(() => service.createUser('invalid-email')).toThrow('Invalid email');
  // ❌ Test fails - createUser doesn't exist
});

// Step 2: GREEN - Minimal implementation
class UserService {
  createUser(email: string): User {
    if (!email.includes('@')) throw new Error('Invalid email');
    return new User(email);
  }
}
// ✅ Test passes

// Step 3: REFACTOR - Improve without breaking test
class UserService {
  createUser(email: string): User {
    this.validateEmail(email);  // Extracted validation
    return new User(email);
  }
  // ✅ Test still passes
}
```

**[Python/pytest]**
```python
# ✅ GOOD - Test first (TDD)

# Step 1: RED - Write failing test
def test_create_user_validates_email():
    with pytest.raises(ValidationError):
        create_user("invalid-email")
    # ❌ Test fails - create_user doesn't exist

# Step 2: GREEN - Minimal implementation
def create_user(email: str) -> User:
    if "@" not in email:
        raise ValidationError("Invalid email")
    return User(email=email)
    # ✅ Test passes

# Step 3: REFACTOR - Improve without breaking test
def create_user(email: str) -> User:
    validate_email(email)  # Extracted validation
    return User(email=email)
    # ✅ Test still passes
```

### Why Tests Must Come First

**Tests-after problems:**

1. **Implementation bias**
   - Tests confirm what code does, not what it should do
   - Bugs baked into tests
   - Missing edge cases

2. **Design issues**
   - Code hard to test (not designed for testability)
   - Tight coupling discovered too late
   - Refactoring required to make testable

3. **Coverage gaps**
   - Temptation to skip "hard to test" paths
   - Error handling overlooked
   - Edge cases ignored

4. **False completion**
   - Feature seems done but untested
   - Integration issues discovered late
   - Delayed feedback loop

**Tests-first benefits:**

1. **Design driver**
   - Forces testable design upfront
   - Identifies coupling before writing code
   - API designed from consumer perspective

2. **Verification**
   - Confirms implementation matches requirements
   - Catches bugs immediately
   - Documents expected behavior

3. **Confidence**
   - Safe refactoring
   - Regression prevention
   - Living documentation

### Integration Testing Strategy

**When to write integration tests:**

- **Before implementation** (TDD approach)
  ```typescript
  // 1. Write failing integration test
  test('user registration flow', async () => {
    const response = await api.post('/register', userData);
    expect(response.status).toBe(201);
    expect(response.body.user).toMatchObject(userData);
  });
  // Test fails - endpoint doesn't exist

  // 2. Implement endpoint
  // 3. Test passes
  ```

- **Alongside unit tests** (test pyramid)
  - Unit tests: Fast, isolated, many
  - Integration tests: Realistic, end-to-end, fewer
  - Both written before/during implementation

**Types of integration tests:**

1. **API integration tests**
   ```typescript
   test('POST /api/users creates user', async () => {
     const response = await request(app)
       .post('/api/users')
       .send({ name: 'Alice', email: 'alice@example.com' });

     expect(response.status).toBe(201);
     expect(response.body).toMatchObject({
       id: expect.any(String),
       name: 'Alice',
       email: 'alice@example.com'
     });

     // Verify database
     const user = await db.users.findById(response.body.id);
     expect(user).toBeDefined();
   });
   ```

2. **Database integration tests**
   ```typescript
   test('user repository saves and retrieves users', async () => {
     const user = await userRepo.create({
       name: 'Alice',
       email: 'alice@example.com'
     });

     const retrieved = await userRepo.findById(user.id);
     expect(retrieved).toEqual(user);
   });
   ```

3. **Component integration tests**
   ```typescript
   test('UserForm submits to API', async () => {
     render(<UserForm />);

     fireEvent.change(screen.getByLabelText('Name'), {
       target: { value: 'Alice' }
     });
     fireEvent.click(screen.getByText('Submit'));

     await waitFor(() => {
       expect(mockApi.post).toHaveBeenCalledWith('/users', {
         name: 'Alice'
       });
     });
   });
   ```

### Test Completeness Checklist

**Before claiming implementation complete:**

```
□ Unit tests written and passing
  - All public functions tested
  - Edge cases covered
  - Error conditions tested

□ Integration tests written and passing
  - End-to-end flows tested
  - External dependencies tested
  - Error handling tested

□ Test coverage meets threshold (e.g., 80%)
  - Line coverage
  - Branch coverage
  - Critical paths covered

□ Tests run in CI/CD pipeline
  - Automated on commits
  - Blocks merge if failing

□ Tests document behavior
  - Clear test names
  - Meaningful assertions
  - Edge cases explained
```

### Gate Function

```
BEFORE claiming implementation complete:
  Ask: "Are tests written?"

  IF no:
    STOP - Not complete
    Write tests before continuing

  Ask: "Do tests cover all requirements?"

  IF no:
    STOP - Incomplete testing
    Add missing test cases

  Ask: "Did I follow TDD (test-first)?"

  IF no:
    WARNING - Consider rewriting with TDD
    Tests may be implementation-biased

  Ask: "Do integration tests pass?"

  IF no:
    STOP - Not ready for integration
    Fix integration issues

  Definition of "complete":
    ✅ Unit tests written and passing
    ✅ Integration tests written and passing
    ✅ Coverage meets threshold
    ✅ Tests run in CI/CD
    ✅ All requirements verified
```

### Recovery: Adding Tests After Implementation

**If tests weren't written first (damage control):**

1. **Stop adding features** - Don't dig deeper hole
2. **Write tests for existing code** - Start with critical paths
3. **Refactor for testability** - Decouple as needed
4. **Adopt TDD going forward** - Prevent future occurrence

**Prioritization:**
- **High priority:** Critical business logic, security, data integrity
- **Medium priority:** Common user flows, API endpoints
- **Low priority:** UI styling, configuration, one-off scripts

**Approach:**
```typescript
// 1. Write characterization tests (document current behavior)
test('current behavior: user registration', async () => {
  const result = await registerUser(userData);
  // Document what currently happens
  expect(result).toMatchSnapshot();
});

// 2. Add specific behavior tests
test('user registration validates email', async () => {
  await expect(registerUser({ email: 'invalid' }))
    .rejects.toThrow('Invalid email');
});

// 3. Refactor safely with test coverage
// Now can refactor with confidence
```

## Prevention Through TDD

**How TDD prevents completeness anti-patterns:**

1. **Incomplete mocks**
   - Writing test first forces understanding of data structures
   - Failing test shows what fields are needed
   - Implementation reveals missing mock fields immediately

2. **Tests as afterthought**
   - TDD makes tests part of implementation process
   - Can't write implementation without test
   - Test-first is the workflow, not optional step

**TDD workflow enforces:**
- Complete understanding of requirements (to write test)
- Complete data structures (test fails if incomplete)
- Complete coverage (tests drive implementation)

See the Test-Driven Development skill for complete TDD workflow (available in the skill library for comprehensive TDD guidance).

## Real-World Impact

**Incomplete mocks:**
- Tests pass, production breaks
- Integration issues discovered late
- False confidence in test suite
- Difficult debugging (mock ≠ reality)

**Tests as afterthought:**
- Bugs escape to production
- Implementation hard to test (design issues)
- Coverage gaps
- Technical debt from day one

**With complete approach:**
- Tests catch integration issues early
- High confidence in test suite
- Testable design from start
- Complete feature = tested feature