|
| 1 | +# Rule: Mandatory Traceability & Structured Logging |
| 2 | + |
| 3 | +## Core Mandate (Non-Negotiable) |
| 4 | + |
| 5 | +**ALL backend code MUST use the `Logger` class from `src/lib/logger.ts` for logging.** This class outputs structured JSON to console AND mirrors every log entry to D1 (`system_logs` table) for persistence and auditability. |
| 6 | + |
| 7 | +### Forbidden Patterns |
| 8 | + |
| 9 | +```typescript |
| 10 | +// ❌ FORBIDDEN — raw console calls bypass D1 mirroring |
| 11 | +console.log("something happened"); |
| 12 | +console.error("something failed:", error); |
| 13 | +console.warn("warning"); |
| 14 | + |
| 15 | +// ❌ FORBIDDEN — truncating error messages or inputs hides root cause |
| 16 | +this.logger.debug(`Running orchestration for: ${input.slice(0, 100)}...`); |
| 17 | +logger.error(`failed`, { body: errBody.substring(0, 200) }); |
| 18 | +``` |
| 19 | + |
| 20 | +### Required Pattern |
| 21 | + |
| 22 | +```typescript |
| 23 | +import { Logger } from '@/lib/logger'; |
| 24 | + |
| 25 | +// ✅ CORRECT — Logger instance per class, with source override |
| 26 | +constructor(protected readonly env: Env, loggerNamespace = 'orchestration/base') { |
| 27 | + this.logger = new Logger(env, loggerNamespace); |
| 28 | +} |
| 29 | +this.logger.debug(`Running orchestration for: ${input}`); |
| 30 | +this.logger.error('Operation failed', { error: error.message, stack: error.stack, responseBody: fullBody }); |
| 31 | +await this.logger.flush(); // MUST flush before returning or throwing |
| 32 | +``` |
| 33 | + |
| 34 | +### Full Error Bodies (MANDATORY) |
| 35 | + |
| 36 | +When logging error responses or inputs, you MUST log the **complete** string or error body. Truncating with `.slice()`, `.substring()`, or any other method is **strictly forbidden**. Truncated strings are useless for debugging and hide root causes. |
| 37 | + |
| 38 | +```typescript |
| 39 | +// ❌ WRONG |
| 40 | +this.logger.debug(`Running orchestration for: ${input.slice(0, 100)}...`); |
| 41 | +const body = await res.text(); |
| 42 | +logger.error('API failed', { body: body.slice(0, 200) }); |
| 43 | + |
| 44 | +// ✅ CORRECT |
| 45 | +this.logger.debug(`Running orchestration for: ${input}`); |
| 46 | +const body = await res.text(); |
| 47 | +logger.error('API failed', { status: res.status, body }); |
| 48 | +``` |
| 49 | + |
| 50 | +## Agent Evaluation Duty (MANDATORY) |
| 51 | + |
| 52 | +Every time an agent evaluates, reviews, modifies, or creates code, it MUST also evaluate: |
| 53 | + |
| 54 | +1. **Traceability Coverage**: Does every significant code path (entry points, error handlers, external API calls, state transitions) have adequate logging? |
| 55 | +2. **Logger Usage**: Is the code using `Logger` from `src/lib/logger.ts`? If it uses raw `console.log`/`console.error`/`console.warn`, the agent MUST migrate it. |
| 56 | +3. **Error Completeness**: Are error messages logged in full, without `.slice()`, `.substring()`, or truncation? |
| 57 | +4. **Flush Discipline**: Is `await logger.flush()` called before every early return, throw, or function exit in error paths? |
| 58 | + |
| 59 | +### When to Add Logging |
| 60 | + |
| 61 | +- **New features**: Every public function must log entry with key parameters |
| 62 | +- **Error handlers**: Every `catch` block must log the full error with stack trace |
| 63 | +- **External calls**: Every `fetch()` to an external API must log the request (URL, method) and response (status, body on failure) |
| 64 | +- **State transitions**: Workflow steps, agent state changes, and provider switches must be logged |
| 65 | + |
| 66 | +### Standard Source Overrides |
| 67 | + |
| 68 | +| Component | Source Override | |
| 69 | +|-----------|----------------| |
| 70 | +| AI Gateway | `'AIGateway'` | |
| 71 | +| Workers AI | `'WorkerAI'` | |
| 72 | +| OpenAI | `'OpenAI'` | |
| 73 | +| Anthropic | `'Anthropic'` | |
| 74 | +| Gemini | `'Gemini'` | |
| 75 | +| AI Router | `'AIRouter'` | |
| 76 | +| Gateway Health | `'GatewayHealth'` | |
| 77 | +| Diagnostician | `'Diagnostician'` | |
| 78 | +| Webhook handler | `'Webhooks'` | |
| 79 | +| Health checks | `'HealthCheck'` | |
| 80 | +| MCP tools | `'MCP:<ToolName>'` | |
| 81 | +| Workflows | `'Workflow:<Name>'` | |
0 commit comments