feat: Add persistent event logging (audit_events) - #22
Conversation
- src/db/audit-events.ts: Event table + AuditService - src/lib/events.ts: Global recordEvent() helper - Task lifecycle: created, started, finished, failed - Chat events: message sent - Auth events: login, logout - Events persisted to SQLite, survive restarts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
@sentry review |
| log.info("Database initialized"); | ||
|
|
||
| // Initialize event recorder for persistent event logging | ||
| initEventRecorder(db.audit); |
There was a problem hiding this comment.
Bug: The db object lacks the audit property, but the code attempts to access it in initEventRecorder and for logging, which will cause a crash on startup and during operations.
Severity: CRITICAL | Confidence: High
🔍 Detailed Analysis
The initDatabase function does not create and attach the audit service to the returned database object. Consequently, db.audit is undefined. The application attempts to access this property at startup in src/index.ts:71 by calling initEventRecorder(db.audit), which will cause a crash. Further crashes will occur during user authentication or task management operations, as functions in src/api/tasks.ts and src/api/auth.ts attempt to call db.audit.log(). The new audit feature is not correctly integrated.
💡 Suggested Fix
In src/db/database.ts, update the initDatabase function. Call initAuditEventsTable(db) to create the table. Then, call createAuditService(db) and assign its return value to an audit property on the object returned by initDatabase. Also, add the audit property to the Database interface.
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: src/index.ts#L71
Potential issue: The `initDatabase` function does not create and attach the `audit`
service to the returned database object. Consequently, `db.audit` is `undefined`. The
application attempts to access this property at startup in `src/index.ts:71` by calling
`initEventRecorder(db.audit)`, which will cause a crash. Further crashes will occur
during user authentication or task management operations, as functions in
`src/api/tasks.ts` and `src/api/auth.ts` attempt to call `db.audit.log()`. The new audit
feature is not correctly integrated.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 507385
Summary
recordEvent()helper for easy usageChanges
src/db/audit-events.ts: Event table schema + AuditServicesrc/lib/events.ts: Global helper with convenience wrapperssrc/index.ts: Initialize event recorder at startupsrc/api/tasks.ts: Log task lifecycle eventssrc/api/auth.ts: Log login/logout eventssrc/chat/manager.ts: Log chat eventsEvent Types
task_created,task_started,task_finished,task_failedchat_sentuser_login,user_logoutagent_heartbeatdeploy,errorTest plan
🤖 Generated with Claude Code