permaweb / aos

An operating system for AO: The hyper parallel computer
https://ao.g8way.io
Other
68 stars 46 forks source link

fix(aos-cli): live feed message sorting #275 #296

Closed PeterFarber closed 1 month ago

PeterFarber commented 1 month ago

Closes #275

Overview

This PR is to address an issue with messages printing out of order in the aos cli.

Solution

The solution was to sort the edges returned from graphql by the ordinate encoded inside of the base64 decoded cursor. Was going to sort by the timestamp encoded inside the cursor but the timestamps seem to not be a direct indicator of message order. Most likely due to the asynchronous nature of the CU?

Changes

Added a single line of code to sort the edges inside the checkLive function in services/connect.js.

// Sort the edges by ordinate value to ensure they are printed in the correct order.
edges = edges.sort((a, b) => JSON.parse(atob(a.cursor)).ordinate - JSON.parse(atob(b.cursor)).ordinate);
TillaTheHun0 commented 1 month ago

The complexity arises when Cron is introduced, which do not have a nonce, but exist "in between" nonces, in a way. And so a message's ordinate can be understood as "my nonce" OR "the most recently scheduled message's nonce". So if there are Cron results, you may need to sort by either timestamp or something like ${ordinate}{$cron}.

Finally, it's worth noting that the cursor received from the CU is meant to be an opaque string used by the server, and simply passed back from the client; it could be changed at the discretion of the CU impl. More broadly speaking, there isn't any guarantee that other CU impl's cursor will contain the same information. So this may work for now, but it may not when other CU implementations are eventually running as part of the network. At the very least, might necessitate a TODO comment explaining this for posterity.