Skip to content

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

@robertocarlous

Description

@robertocarlous

Labels: priority/high, area/agent, feature, bug

Body:

## Summary
Agent decisions (rebalance, scan, alert) are persisted to `agent_logs`, but the current implementation attaches actions to an arbitrary user. This makes analytics, support, and auditing unreliable.

## Problem
In `src/agent/router.ts`:

```ts
// Log to all users for now - in production, could be per-user
const users = await db.user.findMany({
  select: { id: true },
  take: 1, // For now, just log to first user
});
const userId = users[0].id;
await db.agentLog.create({ data: { userId, action, status, ... } });

## Acceptance criteria

 No agent log row is written against a random/first user
 Rebalance logs include correct userId and affected positionId(s)
 System-level scans are distinguishable from user-level actions
 Unit tests for logAgentAction with multiple users
 Integration test: rebalance job creates logs for each impacted user
 Update AgentLog indexes if querying by userId + createdAt becomes hot path

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions