feat(api): add lossless forward message pagination#388
Open
ethfumi wants to merge 1 commit into
Open
Conversation
Add --after-id to the team messages endpoint so polling clients can fetch rows strictly after a cursor. Forward pages select the oldest matching rows before emitting them in ascending order, preventing gaps when more than the requested limit arrives between polls; backward history keeps its existing newest-page behavior. Reject simultaneous before/after cursors and cover both cursor exclusivity and multi-page forward ordering in the API tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
docs/building-on-agmsg.mdは、agmsg の外側から読むコードに対して「ファイルではなくスクリプトを通すこと」を求めています。ストアは実装詳細であって契約ではなく、messages.dbを直接開くコードは storage 軸が入れば警告なく壊れる、と明示されています。GUI アプリ・ダッシュボード・他言語の bot・派生プロジェクトはapi.shを使うことになります。その
api.shに、新着を取りこぼさずに追う手段がありません。team messages は最新行から後ろ向きにしか辿れないため、api.shに従うクライアントは「最新 N 件」を取り直して差分を取るしかなく、2回の polling の間に N 件を超えて届いた分を静かに失います。一方、DB を直接開くコードは
WHERE id > <cursor> ORDER BY idで正しく追えます。前回から何行増えていても取りこぼしません。つまり、ドキュメントが推奨する側のクライアントだけが、取りこぼしのない polling を持っていない状態でした。この差を埋めます。
この必要性は、別マシンの DB を SSH 経由で読む機能を作る中で顕在化しました。remote は
api.sh経由でしか触れないため、ローカルなら当たり前に使えるカーソルが、そこだけ使えませんでした。ただし問題自体は SSH に固有ではなく、api.shを使うすべてのクライアントに共通です。変更内容
カーソルより後ろの行だけを取得する
--after-idを追加しました。forward のページングは、昇順で返す前に「条件に合う最も古い行」から選ぶため、クライアントがどれだけ遅れていてもカーソルを進めるだけで行を飛ばしません。後ろ向きの履歴取得は従来の最新ページ方式のままです。--before-idと--after-idの同時指定は、どちらかを暗黙に優先せずエラーにします。検証
tests/test_api.batsに、カーソルの排他性と、複数ページにまたがる forward の順序を追加しました。