Skip to content

feat(registry): add pagination to GET /agents endpoint - #95

Merged
Bosun-Josh121 merged 4 commits into
clevercon-protocol:mainfrom
Oluwasuyi-Oluwatimilehin-Daniel:feat/registry-pagination
Jul 29, 2026
Merged

feat(registry): add pagination to GET /agents endpoint#95
Bosun-Josh121 merged 4 commits into
clevercon-protocol:mainfrom
Oluwasuyi-Oluwatimilehin-Daniel:feat/registry-pagination

Conversation

@Oluwasuyi-Oluwatimilehin-Daniel

@Oluwasuyi-Oluwatimilehin-Daniel Oluwasuyi-Oluwatimilehin-Daniel commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

#closes #86

Add pagination to GET /agents in registry

Summary

Implements pagination for the GET /agents endpoint to handle unbounded responses as the registry grows. The endpoint now supports limit and offset query parameters with sensible defaults and clamping.

Changes

  • packages/registry/src/server.ts: Added pagination logic with limit/offset params, reputation ordering, and envelope response
  • packages/registry/src/server.test.ts: Added 11 comprehensive pagination test cases
  • docs/development.md: Documented the new pagination API with examples and design rationale

Key Features

  • Default limit: 20, maximum: 100, minimum: 1
  • Default offset: 0, minimum: 0
  • Invalid/negative values are clamped (not rejected)
  • Agents ordered by reputation (descending) before pagination
  • Pagination applied after all existing filters
  • Backward compatible: returns bare array when no pagination params, envelope when pagination requested
  • Response envelope includes: {agents, total, limit, offset}

Testing

  • All 98 tests pass (6 skipped)
  • New test coverage: default page, custom limit/offset, out-of-range offset, cap enforcement, pagination-after-filter, reputation ordering

API Change

When limit or offset are provided, response format changes from bare array to envelope. Existing clients without pagination params continue to work unchanged.
#closes

Summary by CodeRabbit

  • New Features
    • Added optional pagination to the registry’s GET /agents endpoint via limit and offset.
    • Results are consistently ordered by reputation before pagination.
    • Preserves the existing bare-array response when pagination isn’t provided; returns an envelope with agents, total, limit, and offset when it is.
    • Added clamping and edge-case handling for invalid or out-of-range pagination inputs.
  • Documentation
    • Expanded GET /agents documentation with parameters, response formats, and examples.
  • Tests
    • Added pagination test coverage for shapes, clamping, empty pages, and ordering.

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: d6adb2db-d4b5-40b5-9bf5-64f80128dd5b

📥 Commits

Reviewing files that changed from the base of the PR and between 7f6fff4 and 8f700ac.

📒 Files selected for processing (1)
  • packages/registry/src/server.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/registry/src/server.ts

📝 Walkthrough

Walkthrough

The registry’s GET /agents endpoint now supports clamped limit and offset pagination after filtering and reputation ordering. Paginated requests return metadata, while requests without pagination parameters retain the bare-array response. Tests, dependencies, and development documentation cover the new behavior.

Changes

Registry pagination

Layer / File(s) Summary
GET /agents pagination behavior
packages/registry/src/server.ts
The endpoint parses pagination parameters, orders filtered agents by descending reputation, clamps values, and returns paginated metadata or the legacy bare array. The Express app is exported and avoids listening during tests.
Pagination validation and documentation
packages/registry/src/server.test.ts, package.json, docs/development.md
Tests cover pagination semantics and edge cases, Supertest packages support integration tests, and documentation defines parameters, response shapes, examples, and defaults.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant GETAgents
  participant AgentStore
  Client->>GETAgents: Request agents with filters and pagination
  GETAgents->>AgentStore: Load matching agents
  AgentStore-->>GETAgents: Filtered agent records
  GETAgents-->>Client: Ordered page with total, limit, and offset
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding pagination to the registry's GET /agents endpoint.
Linked Issues check ✅ Passed The changes meet #86 by adding clamped limit/offset pagination after filtering and ordering, plus total and documented response behavior.
Out of Scope Changes check ✅ Passed All changes support pagination, testability, or documentation for GET /agents; no unrelated scope is introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/development.md`:
- Around line 181-186: Update the GET /agents examples in docs/development.md so
the paginated first-page request explicitly uses ?limit=20, and revise the
accompanying rationale to state that the maximum applies only when pagination
parameters are provided; preserve the offset example for agents 21–40.

In `@packages/registry/src/server.test.ts`:
- Around line 407-490: Update the GET /agents tests to seed agents through the
store and invoke the HTTP route rather than applying sorting, filtering, or
pagination directly to loadAgents(). Assert each response body shape and values,
including backward-compatible bare arrays, pagination totals, limit/offset
clamping, and the invalid offset=abc case.

In `@packages/registry/src/server.ts`:
- Around line 114-115: Update the offset parsing in the server request handler
so non-numeric values such as “abc” resolve to 0 before calculating
clampedOffset. Preserve the existing behavior for valid offsets and ensure the
serialized offset and pagination logic use the resulting zero value instead of
NaN.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b8777c1d-9091-49c4-85c5-375f5eae8cfc

📥 Commits

Reviewing files that changed from the base of the PR and between a02baea and f88f811.

📒 Files selected for processing (3)
  • docs/development.md
  • packages/registry/src/server.test.ts
  • packages/registry/src/server.ts

Comment thread docs/development.md Outdated
Comment thread packages/registry/src/server.test.ts Outdated
Comment thread packages/registry/src/server.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/registry/src/server.test.ts`:
- Around line 416-422: Update the test case “applies default limit when only
limit param key is provided” to request pagination without a limit parameter,
such as using only ?offset=0, so it exercises the default-limit path. Keep the
assertions verifying a 20-item limit and the existing response and agent-count
expectations.
- Around line 454-460: Update the limit parsing and clamping logic exercised by
the `/agents` handler to distinguish missing or non-numeric values from numeric
zero, preserving the default only for invalid or absent input and clamping
`limit=0` to the minimum value of 1 within the declared 1–100 range. Revise the
test case to expect a response limit of 1 instead of 20.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 10806549-a975-450b-9d5b-e7bf75c642d1

📥 Commits

Reviewing files that changed from the base of the PR and between f88f811 and 98ff33e.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (4)
  • docs/development.md
  • package.json
  • packages/registry/src/server.test.ts
  • packages/registry/src/server.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/registry/src/server.ts
  • docs/development.md

Comment thread packages/registry/src/server.test.ts
Comment thread packages/registry/src/server.test.ts Outdated
Comment on lines +454 to +460
it('clamps limit to minimum (1) when limit is 0 — falls back to default', async () => {
// limit=0 is parsed as 0, which is falsy, so the server substitutes DEFAULT_LIMIT (20)
const res = await request(app).get('/agents?limit=0');
expect(res.status).toBe(200);
expect(res.body.limit).toBe(20); // default limit applied when limit resolves to 0
expect(Array.isArray(res.body.agents)).toBe(true);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Clamp numeric zero to the minimum of 1.

This test enshrines limit=0 → 20, contradicting the PR’s declared 1..100 clamp range. The handler’s falsy fallback has the same defect; distinguish an absent/non-numeric value from numeric zero, then clamp zero to 1.

Proposed test adjustment
-  it('clamps limit to minimum (1) when limit is 0 — falls back to default', async () => {
-    // limit=0 is parsed as 0, which is falsy, so the server substitutes DEFAULT_LIMIT (20)
+  it('clamps limit to minimum (1) when limit is 0', async () => {
     const res = await request(app).get('/agents?limit=0');
     expect(res.status).toBe(200);
-    expect(res.body.limit).toBe(20); // default limit applied when limit resolves to 0
+    expect(res.body.limit).toBe(1);
     expect(Array.isArray(res.body.agents)).toBe(true);
+    expect(res.body.agents).toHaveLength(1);
   });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('clamps limit to minimum (1) when limit is 0 — falls back to default', async () => {
// limit=0 is parsed as 0, which is falsy, so the server substitutes DEFAULT_LIMIT (20)
const res = await request(app).get('/agents?limit=0');
expect(res.status).toBe(200);
expect(res.body.limit).toBe(20); // default limit applied when limit resolves to 0
expect(Array.isArray(res.body.agents)).toBe(true);
});
it('clamps limit to minimum (1) when limit is 0', async () => {
const res = await request(app).get('/agents?limit=0');
expect(res.status).toBe(200);
expect(res.body.limit).toBe(1);
expect(Array.isArray(res.body.agents)).toBe(true);
expect(res.body.agents).toHaveLength(1);
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/registry/src/server.test.ts` around lines 454 - 460, Update the
limit parsing and clamping logic exercised by the `/agents` handler to
distinguish missing or non-numeric values from numeric zero, preserving the
default only for invalid or absent input and clamping `limit=0` to the minimum
value of 1 within the declared 1–100 range. Revise the test case to expect a
response limit of 1 instead of 20.

Distinguish numeric zero from invalid/absent values in limit parsing.
Update tests to verify default limit behavior and zero clamping.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/registry/src/server.test.ts (1)

321-491: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Add a filter-plus-pagination integration test.

The suite covers pagination mechanics but does not verify pagination after an existing filter. Add a request such as GET /agents?status=active&limit=1&offset=1 and assert both the page contents and post-filter total; this catches regressions that slice before filtering or report the unfiltered count.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/registry/src/server.test.ts` around lines 321 - 491, Add an
integration test within the GET /agents pagination describe block that requests
status=active with limit=1 and offset=1, then assert the response contains the
correct second filtered agent and reports the post-filter total of 3. Verify the
paginated envelope and status code, ensuring filtering occurs before slicing and
total calculation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@packages/registry/src/server.test.ts`:
- Around line 321-491: Add an integration test within the GET /agents pagination
describe block that requests status=active with limit=1 and offset=1, then
assert the response contains the correct second filtered agent and reports the
post-filter total of 3. Verify the paginated envelope and status code, ensuring
filtering occurs before slicing and total calculation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2e596c4c-8ae8-41d1-b3b7-edacf24edc83

📥 Commits

Reviewing files that changed from the base of the PR and between 98ff33e and 7f6fff4.

📒 Files selected for processing (2)
  • packages/registry/src/server.test.ts
  • packages/registry/src/server.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/registry/src/server.ts

@Bosun-Josh121

Copy link
Copy Markdown
Collaborator

@Oluwasuyi-Oluwatimilehin-Daniel please fix CI errors

@Oluwasuyi-Oluwatimilehin-Daniel

Oluwasuyi-Oluwatimilehin-Daniel commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@Bosun-Josh121 i already fixed ci

@Bosun-Josh121
Bosun-Josh121 merged commit ec1494b into clevercon-protocol:main Jul 29, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Task]: Add pagination to GET /agents in the registry

2 participants