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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Agent Kickoff Guide

## πŸš€ Starting Your Implementation

This guide helps agents quickly understand how to start working on WidgeTDC improvements.

## πŸ“– Quick Start (5 minutes)

### 1. Read Current State
```bash
# Check main branch status
git checkout main
git pull origin main

# Review current architecture
cat README.md
cat docs/planning/agent-tasks/README.md
```

### 2. Understand Project Structure
```
WidgeTDC/
β”œβ”€β”€ apps/
β”‚   β”œβ”€β”€ backend/          # Node.js/Express backend
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ services/ # Business logic (Memory, SRAG, Evolution, PAL)
β”‚   β”‚   β”‚   β”œβ”€β”€ mcp/      # MCP gateway and tools
β”‚   β”‚   β”‚   └── database/ # SQLite (to be migrated to PostgreSQL)
β”‚   β”‚   └── package.json
β”‚   └── matrix-frontend/     # React frontend
β”‚       β”œβ”€β”€ components/   # UI components
β”‚       β”œβ”€β”€ widgets/      # Individual widgets
β”‚       └── package.json
β”œβ”€β”€ packages/
β”‚   └── shared/
β”‚       └── mcp-types/    # Shared TypeScript types
└── docs/
    └── planning/
        └── agent-tasks/  # Implementation plans (YOU ARE HERE)
```

### 3. Choose Your Phase

**If you're the first agent**:
β†’ Start with [Phase 1: Authentication](./PHASE_1_AUTHENTICATION.md) OR [Phase 9: CI/CD](./PHASE_9_CICD.md) (can run in parallel)

**If Phase 1 is done**:
β†’ Start [Phase 2: PostgreSQL Migration](./PHASE_2_POSTGRESQL_MIGRATION.md)

**If you want observability**:
β†’ [Phase 6: Observability](./PHASE_6_OBSERVABILITY.md) (can run anytime)

### 4. Create Your Branch
```bash
# Pattern: agent/<your-role>/phase<N>-<task-name>
git checkout -b agent/backend-engineer/phase1-auth-service

# Example names:
# agent/backend-architect/phase1-auth-design
# agent/frontend-engineer/phase1-login-form
# agent/devops/phase2-postgres-setup
```

## 🎯 Agent Role Assignments

### Backend Architect
**Responsibilities**: System design, architecture decisions, database schema
**Primary Phases**: 1, 2, 4
**Skills Required**: System architecture, database design, microservices patterns

**Start Here**:
1. Phase 1, Task 1.1: Design Authentication Architecture
2. Phase 2, Task 2.1: Database Schema Analysis & Design

### Backend Engineer
**Responsibilities**: API implementation, business logic, testing
**Primary Phases**: 1, 2, 3, 4, 6, 7, 10
**Skills Required**: Node.js, TypeScript, Express, SQL, Testing

**Start Here**:
1. Phase 1, Task 1.2: Implement Database Schema
2. Phase 1, Task 1.3: Implement Authentication Service
3. Phase 2, Task 2.4: Update Repository Layer

### Frontend Engineer
**Responsibilities**: UI components, state management, responsive design
**Primary Phases**: 1, 5, 7, 8
**Skills Required**: React, TypeScript, CSS, Mobile development

**Start Here**:
1. Phase 1, Task 1.6: Frontend Login Implementation
2. Phase 5: Widget marketplace UI
3. Phase 8: Mobile app development

### Data Engineer
**Responsibilities**: Database migrations, data pipelines, ETL
**Primary Phases**: 2, 3
**Skills Required**: SQL, PostgreSQL, data migration, vector databases

**Start Here**:
1. Phase 2, Task 2.1: Database Schema Analysis & Design
2. Phase 2, Task 2.5: Data Migration Script
3. Phase 3: Vector embeddings setup

### DevOps Engineer
**Responsibilities**: Infrastructure, CI/CD, containerization, monitoring
**Primary Phases**: 4, 6, 9
**Skills Required**: Docker, Kubernetes, GitHub Actions, Prometheus, Grafana

**Start Here**:
1. Phase 9: CI/CD Automation (HIGH PRIORITY - enables all other work)
2. Phase 2, Task 2.2: Set Up PostgreSQL Infrastructure
3. Phase 6: Observability stack

### Security Expert
**Responsibilities**: Security audits, compliance, vulnerability assessment
**Primary Phases**: 1, 10
**Skills Required**: Security best practices, GDPR, penetration testing

**Start Here**:
1. Phase 1: Authentication security review
2. Phase 10: Data privacy and compliance implementation

### QA Engineer
**Responsibilities**: Test strategy, test automation, quality assurance
**Primary Phases**: All phases (cross-cutting)
**Skills Required**: Jest, Playwright, E2E testing, performance testing

**Start Here**:
1. Phase 1: Write test suites for authentication
2. Phase 9: Set up automated testing in CI/CD
3. Review test coverage across all phases

## πŸ“ Implementation Workflow

### Step 1: Read Phase Document
```bash
# Open your assigned phase
cat docs/planning/agent-tasks/PHASE_1_AUTHENTICATION.md
```

**What to look for**:
- [ ] Objective and prerequisites
- [ ] Task breakdown with estimates
- [ ] Deliverables (code, config, docs)
- [ ] Test cases
- [ ] Success criteria

### Step 2: Verify Prerequisites
Example for Phase 1:
```bash
# Check Node.js version
node --version  # Should be >= 18

# Check if backend runs
cd apps/backend
npm install
npm run dev

# Check if frontend runs
cd ../matrix-frontend
npm install
npm run dev
```

### Step 3: Implement Task by Task
```bash
# Create feature branch for each task
git checkout -b agent/backend-engineer/phase1-task1.2-db-schema

# Make changes
# ... implement task ...

# Run tests
npm test

# Commit with descriptive message
git add .
git commit -m "Phase 1, Task 1.2: Implement auth database schema

- Created users, roles, and user_roles tables
- Added foreign key constraints
- Seeded default roles (admin, standard, viewer)
- Tests: Schema creation and constraint validation

Test Results: βœ… All passing (8/8)
"

# Push and create PR
git push origin agent/backend-engineer/phase1-task1.2-db-schema
```

### Step 4: Write Tests FIRST (TDD)
```typescript
// BEFORE implementing feature, write the test

describe('AuthService.login', () => {
  it('should return token for valid credentials', async () => {
    // Arrange
    const username = 'testuser';
    const password = 'password123';

    // Act
    const result = await authService.login(username, password);

    // Assert
    expect(result.accessToken).toBeDefined();
    expect(result.refreshToken).toBeDefined();
  });
});

// NOW implement the feature to make test pass
```

### Step 5: Document As You Go
```typescript
/**
 * Authenticates a user with username and password.
 *
 * @param username - User's username
 * @param password - Plain text password (will be compared with hashed version)
 * @returns Access token, refresh token, and user object
 * @throws {Error} If credentials are invalid
 *
 * @example
 * ```typescript
 * const result = await authService.login('john', 'password123');
 * console.log(result.accessToken); // JWT token
 * ```
 */
async login(username: string, password: string): Promise<AuthResult> {
  // ... implementation
}
```

### Step 6: Create Pull Request
**PR Title Format**: `[Phase X] Task X.Y: <Brief Description>`

**PR Description Template**:
```markdown
## Phase 1, Task 1.3: Implement Authentication Service

### Deliverables
βœ… AuthService class with login, register, refresh methods
βœ… Password hashing with bcrypt
βœ… JWT token generation and verification
βœ… Unit tests with 95% coverage

### Test Results
- Unit Tests: 15/15 passing βœ…
- Integration Tests: 5/5 passing βœ…
- Code Coverage: 95% βœ…

### Breaking Changes
None

### Dependencies
Requires Task 1.2 (database schema) to be merged first

### Screenshots
N/A (backend service)

### Checklist
- [x] Code follows project style guide
- [x] Tests written and passing
- [x] Documentation updated
- [x] No console.log statements
- [x] Error handling implemented
- [x] Security best practices followed

### Review Notes
Please verify:
1. JWT secret handling (using env vars)
2. Password hashing cost factor (set to 10)
3. Token expiration times (15m access, 7d refresh)
```

## πŸ§ͺ Testing Standards

### Unit Tests (Jest)
**Location**: `*.test.ts` next to implementation
**Coverage Target**: β‰₯ 80%
**Run Command**: `npm test`

Example:
```typescript
// apps/backend/src/services/auth/authService.test.ts
import { AuthService } from './authService';

describe('AuthService', () => {
  let authService: AuthService;

  beforeEach(() => {
    authService = new AuthService();
  });

  it('should hash passwords securely', async () => {
    const password = 'mypassword';
    const hash = await authService.hashPassword(password);

    expect(hash).not.toBe(password);
    expect(hash.length).toBeGreaterThan(50);
  });
});
```

### Integration Tests
**Location**: `apps/backend/src/__tests__/integration/`
**Run Command**: `npm run test:integration`

Example:
```typescript
// apps/backend/src/__tests__/integration/auth.test.ts
import request from 'supertest';
import app from '../../index';

describe('Auth Integration', () => {
  it('should login and access protected endpoint', async () => {
    // 1. Login
    const loginRes = await request(app)
      .post('/api/auth/login')
      .send({ username: 'testuser', password: 'password123' });

    expect(loginRes.status).toBe(200);
    const { accessToken } = loginRes.body;

    // 2. Access protected endpoint
    const protectedRes = await request(app)
      .get('/api/memory/entities')
      .set('Authorization', `Bearer ${accessToken}`);

    expect(protectedRes.status).toBe(200);
  });
});
```

### E2E Tests (Playwright)
**Location**: `apps/matrix-frontend/tests/`
**Run Command**: `npm run test:e2e`

Example:
```typescript
// apps/matrix-frontend/tests/auth-flow.spec.ts
import { test, expect } from '@playwright/test';

test('complete auth flow', async ({ page }) => {
  // Navigate to app
  await page.goto('http://localhost:3000');

  // Fill login form
  await page.fill('input[name="username"]', 'testuser');
  await page.fill('input[name="password"]', 'password123');
  await page.click('button:has-text("Login")');

  // Verify redirect to dashboard
  await expect(page).toHaveURL(/.*dashboard/);
  await expect(page.locator('.user-menu')).toContainText('testuser');

  // Logout
  await page.click('.user-menu');
  await page.click('text=Logout');

  // Verify redirect to login
  await expect(page).toHaveURL(/.*login/);
});
```

## πŸ” Code Quality Standards

### TypeScript
```typescript
// βœ… GOOD: Explicit types, no 'any'
function login(username: string, password: string): Promise<AuthResult> {
  // ...
}

// ❌ BAD: Implicit any, no return type
function login(username, password) {
  // ...
}
```

### Error Handling
```typescript
// βœ… GOOD: Specific error types, proper error messages
class InvalidCredentialsError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'InvalidCredentialsError';
  }
}

async function login(username: string, password: string) {
  const user = await findUser(username);
  if (!user) {
    throw new InvalidCredentialsError('User not found');
  }
  // ...
}

// ❌ BAD: Generic errors, no context
async function login(username: string, password: string) {
  const user = await findUser(username);
  if (!user) {
    throw new Error('Error');
  }
}
```

### Async/Await
```typescript
// βœ… GOOD: Async/await with try-catch
async function login(username: string, password: string) {
  try {
    const user = await findUser(username);
    return user;
  } catch (error) {
    logger.error('Login failed', { username, error });
    throw error;
  }
}

// ❌ BAD: Promise chains, no error handling
function login(username, password) {
  return findUser(username)
    .then(user => user);
}
```

## πŸ“ž Communication & Coordination

### Daily Updates
**Format**: Git commit messages
**Frequency**: At least one commit per task

### Blockers
**Report Via**: GitHub issues with `[BLOCKER]` tag
**Include**:
1. Phase and task number
2. Description of blocker
3. What you've tried
4. What help you need

Example:
```markdown
Title: [BLOCKER] Phase 1, Task 1.3: Missing JWT_SECRET environment variable

Description:
Cannot test authentication service because JWT_SECRET is not defined.

Attempted Solutions:
- Checked .env.example
- Searched codebase for .env template

Needed:
- .env.example with JWT_SECRET
- OR documentation on how to generate secrets
- OR default value for development

Blocking:
- Task 1.3: Authentication Service
- Task 1.4: Authorization Middleware
```

### Handoffs
**When completing a phase**, create handoff document:

```markdown
# Phase 1 Handoff Document

## Completed
βœ… All 6 tasks complete
βœ… 100% test coverage
βœ… Documentation updated
βœ… Security audit passed

## What's Ready
- Authentication API (/api/auth/*)
- Authorization middleware
- Login form component
- User/role database

## Known Issues
None

## Next Phase Dependencies Met
βœ… Phase 2 can start (auth not blocking)
βœ… Phase 10 can start (auth foundation ready)

## Environment Variables Added
- JWT_SECRET (required)
- JWT_REFRESH_SECRET (required)
- JWT_EXPIRY (optional, default: 15m)

## Migration Required
Yes - run: npm run migrate:auth

## Notes for Next Agent
- User table has username and email (both unique)
- Default roles seeded on first migration
- Admin user must be created manually (see docs)
```

## πŸŽ“ Learning Resources

### Codebase Patterns
Read these before starting:
- `apps/backend/src/services/memory/memoryRepository.ts` - Repository pattern
- `apps/backend/src/mcp/toolHandlers.ts` - MCP integration pattern
- `apps/matrix-frontend/widgets/AiPalWidget.tsx` - Widget structure

### External Docs
- [Express.js Best Practices](https://expressjs.com/en/advanced/best-practice-performance.html)
- [TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/intro.html)
- [React Testing Library](https://testing-library.com/docs/react-testing-library/intro/)
- [Playwright Testing](https://playwright.dev/docs/intro)

## 🚨 Common Pitfalls

### 1. Not Running Tests Before Committing
```bash
# ❌ DON'T
git add .
git commit -m "Fix"
git push

# βœ… DO
npm test
npm run lint
npm run build
git add .
git commit -m "Phase 1, Task 1.3: Implement AuthService - Tests passing"
git push
```

### 2. Working Directly on Main
```bash
# ❌ DON'T
git checkout main
# ... make changes ...
git push

# βœ… DO
git checkout -b agent/backend-engineer/phase1-task3
# ... make changes ...
git push origin agent/backend-engineer/phase1-task3
# ... create PR ...
```

### 3. Skipping Documentation
```typescript
// ❌ DON'T
async function login(u, p) {
  // code
}

// βœ… DO
/**
 * Authenticates user with username and password.
 * @param username - User's unique username
 * @param password - Plain text password
 * @returns Authentication tokens and user object
 */
async function login(username: string, password: string): Promise<AuthResult> {
  // code
}
```

## βœ… Final Checklist

Before starting:
- [ ] Read phase document completely
- [ ] Verified prerequisites
- [ ] Created feature branch
- [ ] Understand test requirements
- [ ] Know where to report blockers

Before committing:
- [ ] All tests passing
- [ ] Lint passing
- [ ] Documentation updated
- [ ] No console.log or debug code
- [ ] Error handling implemented

Before merging:
- [ ] PR created with proper description
- [ ] Another agent reviewed
- [ ] All CI checks passing
- [ ] Conflicts resolved
- [ ] Handoff document created (if final task)

---

**Ready to start?** Pick your phase and create your branch! πŸš€

**Questions?** Create a GitHub issue with your question.

**Stuck?** Review the phase document test cases - they show expected behavior.