Skip to content

fix(agent): attribute rebalance logs per user/position, not first user#172

Open
bakarezainab wants to merge 1 commit into
Neurowealth:mainfrom
bakarezainab:Fix-agent-action-logging
Open

fix(agent): attribute rebalance logs per user/position, not first user#172
bakarezainab wants to merge 1 commit into
Neurowealth:mainfrom
bakarezainab:Fix-agent-action-logging

Conversation

@bakarezainab

Copy link
Copy Markdown

Problem: logAgentAction queried db.user.findMany({ take: 1 }) and pinned every agent log row to an arbitrary first user, making auditing and per-user analytics unreliable.

Changes:

  • schema.prisma: make AgentLog.userId nullable (system-level actions produce rows with userId=null, distinguishable from user-level ones); add positionId field; add composite index [userId, createdAt] for hot query paths
  • prisma/migrations/20260617000000_fix_agent_log_attribution: SQL migration altering agent_logs accordingly
  • router.ts: remove first-user lookup from logAgentAction; accept optional userId + positionId params; in triggerRebalance, fetch all affected positions and write one log row per (userId, positionId) pair; system-level REBALANCE/ANALYZE calls leave userId=null
  • tests/unit/agent/logAgentAction.test.ts: 9 unit tests covering system-level (null userId), user-level attribution, multiple-user isolation, and error-handling
  • tests/integration/agent/rebalance.integration.test.ts: 6 integration tests verifying per-user log creation for each impacted position, no arbitrary-user attribution, deduplication guard, and null-userId system-level fallback

All 50 tests pass (5 suites), tsc --noEmit clean

Closes #166

Problem: logAgentAction queried db.user.findMany({ take: 1 }) and pinned
every agent log row to an arbitrary first user, making auditing and
per-user analytics unreliable.

Changes:
- schema.prisma: make AgentLog.userId nullable (system-level actions
  produce rows with userId=null, distinguishable from user-level ones);
  add positionId field; add composite index [userId, createdAt] for
  hot query paths
- prisma/migrations/20260617000000_fix_agent_log_attribution: SQL
  migration altering agent_logs accordingly
- router.ts: remove first-user lookup from logAgentAction; accept
  optional userId + positionId params; in triggerRebalance, fetch all
  affected positions and write one log row per (userId, positionId)
  pair; system-level REBALANCE/ANALYZE calls leave userId=null
- tests/unit/agent/logAgentAction.test.ts: 9 unit tests covering
  system-level (null userId), user-level attribution, multiple-user
  isolation, and error-handling
- tests/integration/agent/rebalance.integration.test.ts: 6 integration
  tests verifying per-user log creation for each impacted position,
  no arbitrary-user attribution, deduplication guard, and null-userId
  system-level fallback

All 50 tests pass (5 suites), tsc --noEmit clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[High] Fix agent action logging to attribute rebalances per user/position`

1 participant