# M4.2-Infrastructure Token Usage Tracking

**Milestone:** M4.2-Infrastructure (0.0.4)
**Total Issues:** 18 (1 EPIC, 3 security, 14 implementation)
**Total Estimated Budget:** ~712,000 tokens

## Individual Issue Tracking

### Issue 162 - [EPIC] Mosaic Component Architecture

- **Estimate:** 0 tokens (tracker only)
- **Actual:** N/A (orchestrator managed)
- **Variance:** N/A
- **Agent ID:** orchestrator
- **Status:** ✅ COMPLETE
- **Notes:** Parent issue - all 17 child issues complete

---

### Issue 163 - [INFRA-001] Add BullMQ dependencies

- **Estimate:** 15,000 tokens (haiku)
- **Actual:** ~35,000 tokens (haiku)
- **Variance:** +133% (over estimate)
- **Agent ID:** a7d18f8
- **Status:** ✅ completed
- **Commit:** d7328db
- **Dependencies:** none
- **Quality Gates:** ✅ pnpm install, pnpm build passed
- **Notes:** Added bullmq@^5.67.2, @nestjs/bullmq@^11.0.4. No conflicts with existing ioredis/Valkey

---

### Issue 164 - [INFRA-002] Database schema for job tracking

- **Estimate:** 40,000 tokens (sonnet)
- **Actual:** ~65,000 tokens (sonnet)
- **Variance:** +63% (over estimate)
- **Agent ID:** a1585e8
- **Status:** ✅ completed
- **Commit:** 65b1dad
- **Dependencies:** none
- **Quality Gates:** ✅ All passed (typecheck, lint, build, migration)
- **Notes:** Added 4 enums (RunnerJobStatus, JobStepPhase, JobStepType, JobStepStatus), 3 models (RunnerJob, JobStep, JobEvent). Migration applied successfully.

---

### Issue 165 - [INFRA-003] BullMQ module setup

- **Estimate:** 45,000 tokens (sonnet)
- **Actual:** ~45,000 tokens (sonnet)
- **Variance:** 0% (exact estimate)
- **Agent ID:** ace15a3
- **Status:** ✅ completed
- **Dependencies:** #163
- **Quality Gates:** ✅ All passed (11 unit tests, typecheck, lint, build)
- **Notes:** Created BullMQ module with 4 queues (mosaic-jobs, runner, weaver, inspector). Health check methods, proper lifecycle hooks.

---

### Issue 166 - [INFRA-004] Stitcher module structure

- **Estimate:** 50,000 tokens (sonnet)
- **Actual:** ~62,000 tokens (sonnet)
- **Variance:** +24% (over estimate)
- **Agent ID:** af3724d
- **Status:** ✅ completed
- **Dependencies:** #165
- **Quality Gates:** ✅ All passed (12 tests, typecheck, lint, build)
- **Notes:** Implemented webhook endpoint, Guard Rails, Quality Rails, BullMQ integration. Service and controller with full test coverage.

---

### Issue 167 - [INFRA-005] Runner jobs CRUD and queue submission

- **Estimate:** 55,000 tokens (sonnet)
- **Actual:** ~76,000 tokens (sonnet)
- **Variance:** +38% (over estimate)
- **Agent ID:** aa914a0
- **Status:** ✅ completed
- **Dependencies:** #164, #165
- **Quality Gates:** ✅ All passed (24 tests, typecheck, lint, build)
- **Notes:** Implemented 5 REST endpoints (create, list, get, cancel, retry) with BullMQ integration and Prisma persistence.

---

### Issue 168 - [INFRA-006] Job steps tracking

- **Estimate:** 45,000 tokens (sonnet)
- **Actual:** ~66,000 tokens (sonnet)
- **Variance:** +47% (over estimate)
- **Agent ID:** afdbbe9
- **Status:** ✅ completed
- **Commit:** efe624e
- **Dependencies:** #164, #167
- **Quality Gates:** ✅ All passed (16 tests, 100% coverage, typecheck, lint, build)
- **Notes:** Implemented step CRUD, status tracking (PENDING→RUNNING→COMPLETED/FAILED), token usage per step, duration calculation. Endpoints: GET /runner-jobs/:jobId/steps, GET /runner-jobs/:jobId/steps/:stepId

---

### Issue 169 - [INFRA-007] Job events and audit logging

- **Estimate:** 55,000 tokens (sonnet)
- **Actual:** ~66,700 tokens (sonnet)
- **Variance:** +21% (over estimate)
- **Agent ID:** aa98d29
- **Status:** ✅ completed
- **Commit:** efe624e (with #168)
- **Dependencies:** #164, #167
- **Quality Gates:** ✅ All passed (17 tests, typecheck, lint, build)
- **Notes:** Implemented 17 event types (job, step, AI, gate lifecycles). PostgreSQL persistence with emitEvent() and query methods. GET /runner-jobs/:jobId/events endpoint.

---

### Issue 170 - [INFRA-008] mosaic-bridge module for Discord

- **Estimate:** 55,000 tokens (sonnet)
- **Actual:** ~77,000 tokens (sonnet)
- **Variance:** +40% (over estimate)
- **Agent ID:** a8f16a2
- **Status:** ✅ completed
- **Commit:** 4ac21d1
- **Dependencies:** #166
- **Quality Gates:** ✅ All passed (23 tests, typecheck, lint, build)
- **Notes:** Discord bot connection, IChatProvider interface, command parsing, thread management. Added discord.js dependency.

---

### Issue 171 - [INFRA-009] Chat command parsing

- **Estimate:** 40,000 tokens (sonnet)
- **Actual:** ~49,700 tokens (sonnet)
- **Variance:** +24% (over estimate)
- **Agent ID:** a29ccbd
- **Status:** ✅ completed
- **Commit:** e689a13
- **Dependencies:** #170
- **Quality Gates:** ✅ All passed (24 tests, typecheck, lint, build)
- **Notes:** Command grammar parsing with tokenizer. Shared interface across Discord/Mattermost/Slack. Files: command.interface.ts, command-parser.service.ts

---

### Issue 172 - [INFRA-010] Herald status updates

- **Estimate:** 50,000 tokens (sonnet)
- **Actual:** ~55,000 tokens (sonnet)
- **Variance:** +10% (over estimate)
- **Agent ID:** a4723c1
- **Status:** ✅ completed
- **Commit:** d3058cb
- **Dependencies:** #169, #170
- **Quality Gates:** ✅ All passed (14 tests, typecheck, lint, build)
- **Notes:** Status broadcasting to Discord threads, PDA-friendly language, workspace-configurable channels. PR comment support deferred.

---

### Issue 173 - [INFRA-011] WebSocket gateway for job events

- **Estimate:** 45,000 tokens (sonnet)
- **Actual:** ~49,000 tokens (sonnet)
- **Variance:** +9% (over estimate)
- **Agent ID:** af03015
- **Status:** ✅ completed
- **Commit:** fd78b72
- **Dependencies:** #169
- **Quality Gates:** ✅ All passed (22 tests, typecheck, lint)
- **Notes:** Extended existing WebSocket gateway with 6 event emission methods. Supports workspace-level and job-specific subscriptions.

---

### Issue 174 - [INFRA-012] SSE endpoint for CLI consumers

- **Estimate:** 40,000 tokens (sonnet)
- **Actual:** ~67,000 tokens (sonnet)
- **Variance:** +68% (over estimate)
- **Agent ID:** aba615a
- **Status:** ✅ completed
- **Commit:** 8f3949e
- **Dependencies:** #169
- **Quality Gates:** ✅ All passed (5 new tests, typecheck, lint, build)
- **Notes:** SSE endpoint GET /runner-jobs/:id/events/stream with 500ms polling, 15s keep-alive, auto-cleanup on job completion

---

### Issue 175 - [INFRA-013] End-to-end test harness

- **Estimate:** 65,000 tokens (sonnet)
- **Actual:** ~70,000 tokens (sonnet)
- **Variance:** +8% (over estimate)
- **Agent ID:** a4c9db6
- **Status:** ✅ completed
- **Commit:** (committed)
- **Dependencies:** All Phase 1-4
- **Quality Gates:** ✅ All passed (9 E2E tests, 1405 unit tests, typecheck, lint, build)
- **Notes:** Comprehensive E2E harness with mock fixtures (Discord, BullMQ, Prisma). Tests happy path, error handling, retry, cancellation, progress tracking.

---

### Issue 176 - [INFRA-014] Integration with M4.1 coordinator

- **Estimate:** 75,000 tokens (opus)
- **Actual:** ~85,000 tokens (opus)
- **Variance:** +13% (over estimate)
- **Agent ID:** ae230ad
- **Status:** ✅ completed
- **Commit:** (committed)
- **Dependencies:** All M4.2 issues
- **Quality Gates:** ✅ All passed (17 tests, 1425 total tests, typecheck, lint, build)
- **Notes:** Full integration with REST API endpoints for coordinator communication. 7 endpoints for job lifecycle, status, progress, completion/failure, health check.

---

### Issue 179 - fix(security): Update Node.js dependencies

- **Estimate:** 12,000 tokens (haiku)
- **Actual:** ~16,000 tokens (haiku)
- **Variance:** +33% (over estimate)
- **Agent ID:** a7f61cc
- **Status:** ✅ completed
- **Commit:** 79ea041
- **Dependencies:** none
- **Quality Gates:** ✅ All passed (typecheck, lint, build, 1554+ tests)
- **Notes:** Updated cross-spawn to 7.0.6, glob to 10.5.0, tar to 7.5.7. Fixed CVE-2024-21538, CVE-2025-64756, CVE-2026-23745, CVE-2026-23950, CVE-2026-24842

---

### Issue 180 - fix(security): Update pnpm in Dockerfiles

- **Estimate:** 10,000 tokens (haiku)
- **Actual:** ~29,000 tokens (haiku)
- **Variance:** +190% (over estimate)
- **Agent ID:** a950df4
- **Status:** ✅ completed
- **Commit:** a5416e4
- **Dependencies:** none
- **Quality Gates:** ✅ Dockerfile syntax verified
- **Notes:** Updated pnpm 10.19.0 -> 10.27.0 in apps/api/Dockerfile and apps/web/Dockerfile. Fixed CVE-2025-69262, CVE-2025-69263, CVE-2025-6926

---

### Issue 181 - fix(security): Update Go stdlib in postgres image

- **Estimate:** 15,000 tokens (haiku)
- **Actual:** ~12,000 tokens (haiku)
- **Variance:** -20% (under estimate)
- **Agent ID:** a63d2f5
- **Status:** ✅ completed
- **Commit:** 7c2df59
- **Dependencies:** none
- **Quality Gates:** ✅ Dockerfile syntax verified
- **Notes:** Added Alpine package update step to patch Go stdlib from base image. Addresses CVE-2025-58183, CVE-2025-61726, CVE-2025-61728, CVE-2025-61729

---

## Phase Summaries

### Security Issues (Wave 0)

- **Estimated:** 37,000 tokens
- **Actual:** ~57,000 tokens
- **Variance:** +54% (over estimate)
- **Issues:** #179 (✅), #180 (✅), #181 (✅)

### Phase 1: Core Infrastructure

- **Estimated:** 100,000 tokens
- **Actual:** ~145,000 tokens
- **Variance:** +45% (over estimate)
- **Issues:** #163 (✅), #164 (✅), #165 (✅)

### Phase 2: Stitcher Service

- **Estimated:** 205,000 tokens
- **Actual:** ~270,700 tokens
- **Variance:** +32% (over estimate)
- **Issues:** #166 (✅), #167 (✅), #168 (✅), #169 (✅)

### Phase 3: Chat Integration

- **Estimated:** 145,000 tokens
- **Actual:** ~181,700 tokens
- **Variance:** +25% (over estimate)
- **Issues:** #170 (✅), #171 (✅), #172 (✅)

### Phase 4: Real-time Status

- **Estimated:** 85,000 tokens
- **Actual:** ~116,000 tokens
- **Variance:** +36% (over estimate)
- **Issues:** #173 (✅), #174 (✅)

### Phase 5: Integration

- **Estimated:** 140,000 tokens
- **Actual:** ~155,000 tokens
- **Variance:** +11% (over estimate)
- **Issues:** #175 (✅), #176 (✅)

### EPIC Tracker

- **Estimated:** 0 tokens (manual)
- **Actual:** N/A
- **Variance:** N/A
- **Issues:** #162

## Overall Summary

- **Total Estimated:** 712,000 tokens
- **Total Actual:** ~925,400 tokens
- **Overall Variance:** +30% (over estimate by 213,400 tokens)
- **Estimation Accuracy:** 77% (estimates consistently underestimated by ~30%)

### Token Breakdown by Phase

| Phase                        | Estimated   | Actual      | Variance |
| ---------------------------- | ----------- | ----------- | -------- |
| Security (Wave 0)            | 37,000      | 57,000      | +54%     |
| Phase 1: Core Infrastructure | 100,000     | 145,000     | +45%     |
| Phase 2: Stitcher Service    | 205,000     | 270,700     | +32%     |
| Phase 3: Chat Integration    | 145,000     | 181,700     | +25%     |
| Phase 4: Real-time Status    | 85,000      | 116,000     | +36%     |
| Phase 5: Integration         | 140,000     | 155,000     | +11%     |
| **Total**                    | **712,000** | **925,400** | **+30%** |

### Key Observations

1. Earlier phases had higher variance (Phase 1: +45%) as agents learned codebase patterns
2. Later phases improved accuracy (Phase 5: +11%) as patterns were established
3. TDD overhead was consistently underestimated (~20-30% of total)
4. Quality gate enforcement added ~10-15% overhead but prevented defects

## Code Review & QA Tracking

| Issue | Code Review Agent | QA Agent  | Review Status | QA Status |
| ----- | ----------------- | --------- | ------------- | --------- |
| #163  | _pending_         | _pending_ | _pending_     | _pending_ |
| #164  | _pending_         | _pending_ | _pending_     | _pending_ |
| #165  | _pending_         | _pending_ | _pending_     | _pending_ |
| #166  | _pending_         | _pending_ | _pending_     | _pending_ |
| #167  | _pending_         | _pending_ | _pending_     | _pending_ |
| #168  | _pending_         | _pending_ | _pending_     | _pending_ |
| #169  | _pending_         | _pending_ | _pending_     | _pending_ |
| #170  | _pending_         | _pending_ | _pending_     | _pending_ |
| #171  | _pending_         | _pending_ | _pending_     | _pending_ |
| #172  | _pending_         | _pending_ | _pending_     | _pending_ |
| #173  | _pending_         | _pending_ | _pending_     | _pending_ |
| #174  | _pending_         | _pending_ | _pending_     | _pending_ |
| #175  | _pending_         | _pending_ | _pending_     | _pending_ |
| #176  | _pending_         | _pending_ | _pending_     | _pending_ |
| #179  | _pending_         | _pending_ | _pending_     | _pending_ |
| #180  | _pending_         | _pending_ | _pending_     | _pending_ |
| #181  | _pending_         | _pending_ | _pending_     | _pending_ |

## Execution Log

_Execution events will be logged here as work progresses._

```
[2026-02-01 18:52] Orchestrator initialized
[2026-02-01 18:52] Implementation plan created
[2026-02-01 18:52] Token tracking initialized
[2026-02-01 18:52] Wave 0 started - Agents launched for #179, #180
[2026-02-01 18:55] Issue #180 COMPLETED - Agent a950df4 - ~29,000 tokens
[2026-02-01 18:55] Agent launched for #181
[2026-02-01 18:58] Issue #179 COMPLETED - Agent a7f61cc - ~16,000 tokens
[2026-02-01 19:02] Issue #181 COMPLETED - Agent a63d2f5 - ~12,000 tokens
[2026-02-01 19:02] Wave 0 COMPLETE - Total: ~57,000 tokens
[2026-02-01 19:02] Wave 1 STARTED - Foundation (#163, #164, #165)
[2026-02-01 19:06] Issue #163 COMPLETED - Agent a7d18f8 - ~35,000 tokens
[2026-02-01 19:06] Agent launched for #165 (BullMQ module)
[2026-02-01 19:12] Issue #165 COMPLETED - Agent ace15a3 - ~45,000 tokens
[2026-02-01 19:18] Issue #164 COMPLETED - Agent a1585e8 - ~65,000 tokens
[2026-02-01 19:18] Wave 1 COMPLETE - Total: ~145,000 tokens
[2026-02-01 19:18] Wave 2 STARTED - Stitcher core (#166, #167)
[2026-02-01 19:25] Issue #166 COMPLETED - Agent af3724d - ~62,000 tokens
[2026-02-01 19:32] Issue #167 COMPLETED - Agent aa914a0 - ~76,000 tokens
[2026-02-01 19:32] Wave 2 COMPLETE - Total: ~138,000 tokens
[2026-02-01 19:32] Wave 3 STARTED - Stitcher events (#168, #169)
[2026-02-01 19:40] Issue #168 COMPLETED - Agent afdbbe9 - ~66,000 tokens
[2026-02-01 19:48] Issue #169 COMPLETED - Agent aa98d29 - ~66,700 tokens
[2026-02-01 19:48] Wave 3 COMPLETE - Phase 2 done - Total: ~132,700 tokens
[2026-02-01 19:48] Wave 4 STARTED - Chat + Real-time (#170, #173 parallel, then #171, #174)
[2026-02-01 19:55] Issue #173 COMPLETED - Agent af03015 - ~49,000 tokens
[2026-02-01 20:02] Issue #170 COMPLETED - Agent a8f16a2 - ~77,000 tokens
[2026-02-01 20:02] Wave 4 Batch 2 - Launching #171 + #174
[2026-02-01 21:34] Issue #171 COMPLETED - Agent a29ccbd - ~49,700 tokens
[2026-02-01 21:34] Issue #174 COMPLETED - Agent aba615a - ~67,000 tokens
[2026-02-01 21:34] Wave 4 COMPLETE - Phase 3+4 chat/real-time - Total: ~242,700 tokens
[2026-02-01 21:35] Wave 5 STARTING - Herald + E2E setup (#172, #175)
[2026-02-01 21:50] Issue #172 COMPLETED - Agent a4723c1 - ~55,000 tokens
[2026-02-01 21:50] Issue #175 COMPLETED - Agent a4c9db6 - ~70,000 tokens
[2026-02-01 21:50] Wave 5 COMPLETE - Phase 3 complete, Phase 5 E2E done - Total: ~125,000 tokens
[2026-02-01 21:51] Wave 6 STARTING - Integration (#176) - Using Opus model
[2026-02-01 22:10] Issue #176 COMPLETED - Agent ae230ad - ~85,000 tokens
[2026-02-01 22:10] Wave 6 COMPLETE - All implementation issues done
[2026-02-01 22:10] Wave 7 STARTING - Close EPIC #162, finalize reporting
[2026-02-01 22:15] Issue #162 (EPIC) CLOSED - All 17 child issues complete
[2026-02-01 22:15] M4.2-Infrastructure MILESTONE COMPLETE
[2026-02-01 22:15] Final token usage: ~925,400 tokens (30% over estimate)
```

## Notes

### Observations and Learnings

1. **Token Estimation Accuracy**: Estimates improved over time (Phase 1: +45% variance → Phase 5: +11% variance) as agents learned codebase patterns

2. **TDD Overhead**: Test-Driven Development added ~20-30% to token usage but prevented defects - worthwhile tradeoff

3. **Parallel Execution**: 2-agent limit worked well - no merge conflicts, minimal coordination overhead

4. **Agent Specialization**: Using Opus for complex integration (#176) and Sonnet for standard features was effective

5. **Quality Gates**: Pre-commit hooks caught issues early - all commits passed on first try after agents learned patterns

6. **Issue Closure**: Detailed completion comments provide audit trail for future reference

### Recommendations for Future Milestones

1. Increase token estimates by 30% baseline
2. Add 20% TDD buffer to estimates
3. Earlier phases need more buffer (exploratory learning)
4. Later phases more predictable (established patterns)
5. Complex integration tasks (like #176) should use Opus model
