DECLARE
@RowLimit INT = IIF(@Limit IS NULL OR @Limit <= 0, 200, @Limit);

SELECT *
FROM [dbo].[AgentOutcomeSignals]
WHERE [WorkspaceId] = @WorkspaceId
  AND (@AgentId IS NULL
   OR [AgentId] = @AgentId)
  AND (@DecisionKey IS NULL
   OR [DecisionKey] = @DecisionKey)
  AND (@Kind IS NULL
   OR [Kind] = @Kind)
  AND (@Source IS NULL
   OR [Source] = @Source)
  AND (@OccurredAfter IS NULL
   OR [OccurredAt] >= @OccurredAfter)
ORDER BY [OccurredAt] DESC
OFFSET 0 ROWS FETCH NEXT @RowLimit ROWS ONLY;
