Skip to content

Paginate automation history with limit and cursor - #454

Closed
sanketpatel32 wants to merge 1 commit into
useagenthq:mainfrom
sanketpatel32:feat/automation-history-pagination
Closed

sanketpatel32 wants to merge 1 commit into
useagenthq:mainfrom
sanketpatel32:feat/automation-history-pagination

Conversation

@sanketpatel32

@sanketpatel32 sanketpatel32 commented Nov 22, 2026 •

Copy link
Copy Markdown

Summary

An automation that fires every minute accumulates tens of thousands of schedule_firings rows, and GET /api/automations/:id/history (plus the bot routine history route, which reads the same firing log) returned every row ever, each joined with its run summary. This PR bounds both routes with keyset pagination, reusing the same versioned base64url cursor convention as the artifacts list.

  • listFirings (backend/src/schedules/repo.ts) now takes limit (default 50, capped at 200, clamped to a minimum of 1) and an optional before cursor, reads limit + 1 rows, and returns a page: at most limit firings plus nextCursor when another page exists.
  • The cursor is a keyset over (fired_at, id), the same composite sort the history already used (fired_at then id, newest first), so pages hold a stable order even when firings share a timestamp. It is encoded as versioned base64url JSON (backend/src/schedules/firing-cursor.ts) modeled on artifacts/list-cursor.ts, carrying the PostgreSQL timestamp text so microsecond precision survives.
  • Both routes accept ?limit= and ?before= and answer { firings, has_more, next_cursor }; has_more is true exactly when an extra row existed beyond the page. next_cursor is passed back as ?before= to walk older firings. A malformed before is a 400 on both routes.
  • has_more/next_cursor are additive: existing clients that read firings keep working.
  • The gateway automation_history tool (backend/src/knowledge/gateway/automation-tools.ts), the only other listFirings caller, consumes the page shape and now surfaces has_more/next_cursor in its structured result; its bounded text rendering is unchanged.

Closes #200

Test plan

New end-to-end coverage in backend/test/firing-pages.test.ts seeds 120 real firings through the app and walks the actual HTTP routes on a real Postgres (18.7 + pgvector 0.8.7): the limit is respected and the default is 50, has_more flips false on the last page, before returns only older firings with none repeated or missing across all pages, a page cut inside a same-timestamp group still resumes correctly, malformed cursors get 400 on both routes, and the bot routine history pages identically.

$ TEST_ADMIN_URL=... TEST_DATABASE_URL=... bun test --max-concurrency=1 test/firing-pages.test.ts
 5 pass
 0 fail
 108 expect() calls

Direct neighbors also pass on the same database:

$ bun test test/schedules.test.ts test/bots-routines.test.ts test/schedules-*.test.ts
 63 pass
 0 fail

Other checks (run on Windows, real Postgres on port 5444):

  • bun run typecheck: backend tsc --noEmit exits 0; frontend tsc --noEmit exits 0; all 13 packages/* tsc --noEmit pass.
  • bun test scripts/agent-docs.test.ts: 5 pass.
  • cd frontend && bun run lint: 0 errors (248 pre-existing warnings).
  • cd frontend && bun run test: 2010 pass, 3 fail, all pre-existing environment failures in files this change does not touch (two CRLF-sensitive source assertions in session-rail-layout.test.ts, one ghostty-web fetch() URL is invalid in terminal-surface.test.ts).
  • cd backend && bun run test: 3593 pass, 56 fail. Every failure is environmental to this Windows box (no /bin/sh, no ~/.bun/bin/bun.exe for spawned tests, : in temp dir names, Unix permission bits, CRLF checkouts, missing engine binaries, one flaky 30 s timeout in nextOccurrence); the representative subset was rerun on a clean upstream/main worktree and fails identically there. No failure is in the firing/history path: schedules.test.ts, bots-routines.test.ts, automation-tools.test.ts and the new firing-pages.test.ts all pass.

End-to-end demo of the new contract (from the test run, real route responses over 120 seeded firings):

GET /api/automations/:id/history                    -> 50 firings, has_more: true,  next_cursor: eyp...
GET /api/automations/:id/history?limit=50&before=.. -> 50 firings, has_more: true,  next_cursor: eyp...
GET /api/automations/:id/history?limit=50&before=.. -> 20 firings, has_more: false, next_cursor: null
GET /api/automations/:id/history?before=@@@         -> 400 { error: "invalid before cursor" }

The 120 firings paged in three limit=50 requests return exactly the same ids in exactly the same order as one unbounded query, and ten firings sharing one fired_at to the microsecond straddle a page cut without duplication or loss.


Get evidence
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

GET /api/automations/:id/history and the bot routine history route now return at most a limit of firings (default 50, capped at 200) plus has_more and next_cursor. The cursor is a versioned keyset over (fired_at, id) passed back as ?before=, matching the artifacts list cursor convention, so frequent schedules no longer return every firing ever.

Closes useagenthq#200
@sanketpatel32
sanketpatel32 force-pushed the feat/automation-history-pagination branch from 20a567e to 6c43d42 Compare November 22, 2026 02:31
@sanketpatel32

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 22, 2026
@github-actions

Copy link
Copy Markdown

Thanks @sanketpatel32 for picking this up. #452 fixed the same issue (#200) and was merged first, so this one is closed as superseded.

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return automation history in pages, not every firing

2 participants