DEV Community

minia2a
minia2a

Posted on Edited on Originally published at minia2a.uk

A count that counts the slice is not a count

This content reflects minia2a as of its publication date and is kept for the record. Current model: x402 pay-per-call in USDC on Base only, 5 free trial calls per signed wallet, no credits and no top-up rail.

A count that counts the slice is not a count

2026-11-27 · nine defects found in a live pay-per-call catalogue

A paid endpoint answers with an array of 50 NFTs and a field nftCount: 50. The wallet it was asked about has more. Nothing else in the response says so — no flag, no cursor, no "showing 50 of N". The buyer, usually an agent, budgeted one call, got a number that looks like an answer, and moved on with a conclusion built on part of the data.

That is not a wrong answer. A wrong answer gets caught: the value fails a schema check, contradicts another field, or crashes the caller. This one passes every check the caller can run, because it is exactly the shape of a correct answer. It is only smaller.

On 2026-11-27 we swept our own catalogue for it — 96 output-side slice(0, N) sites and 94 count: something.length sites across the deployed handlers — and found nine live endpoints doing it. This is what the family looks like, why per-endpoint smoke tests never catch it, and the disclosure shape that makes it impossible.

The shape

const items   = (data.items || []).slice(0, 50);   // cap the payload
return { ok: true, nftCount: items.length, nfts: items };
//                                ^^^^^^^^^^^^ counts the slice
Enter fullscreen mode Exit fullscreen mode

nftCount is a promise about the wallet; items.length is a fact about the array. After the slice those are different numbers, and the field is named for the one it is not. The buyer reads how many NFTs this wallet has and is told how many we decided to send.

Two smaller variants of the same lie turned up in the same sweep. One returns a count from upstream but truncates the body with no marker at all: the page text stops at 50,000 characters and the response has no field that distinguishes "this page is 50,000 characters" from "this page is longer and you are reading a prefix". The other cuts a banner to 500 characters and calls it the banner.

Why the buyer cannot notice

Compare it with a nearer-miss failure we fixed earlier the same day: an endpoint returned contentLength: 120000 next to an html string that had been trimmed. That is at least self-comparable — a careful caller can check html.length against contentLength and see the gap.

nftCount: 50 gives the caller nothing to compare against. There is no second number. The only way to learn that another page exists is to call a different API (the upstream explorer) and read its pagination token — which is not something a caller paying for your abstraction should have to do.

Smoke tests do not catch it either. A test wallet with 50 NFTs returns 50 of 50: correct. The defect only exists for wallets past the cap, and the bigger the wallet, the more wrong the answer — which is exactly backwards from how damage should scale.

The general rule. Any cap on an output array or string needs a field that says the cap was hit. Silence is a claim that the cap was not hit, and the server is the only party that knows.

The disclosure shape

What we now emit, on all nine:

{
  "ok": true,
  "nftCount": 312,          // from the upstream answer, not from the array
  "returnedNfts": 50,       // what is actually in `nfts`
  "nftsTruncated": true,    // the cap was hit
  "hasMore": true,          // the upstream says another page exists
  "nfts": [ /* 50 items */ ]
}
Enter fullscreen mode Exit fullscreen mode

Four fields, and each answers a question the buyer would otherwise have to guess at:

  • nftCount — how many exist (taken from upstream, never from the sliced array);
  • returnedNfts — how many you got;
  • nftsTruncated — whether the two differ because of our cap;
  • hasMore — whether the upstream itself has another page (only when the upstream exposes pagination; a bare "there's more" without a way to get it is a taunt, not a disclosure).

For string bodies the pair is returnedHtmlChars and htmlTruncated, plus a switch (raw: false) for callers who wanted the structure and not the body in the first place. Live check after the change: a wallet whose upstream answer carried a non-empty next_page_params now reports hasMore: true; a JSON object with 120 keys reports keysTruncated: true with keys capped at 50.

The second shape: an error page served as content

The same sweep turned up a sibling that is worse in one way. One endpoint fetched /robots.txt and, when the host answered 404 with an HTML error page, returned that HTML as the robots body. A caller parsing it as robots directives sees markup it cannot parse — or, worse, extracts the wrong rule from a page that happens to contain the word Disallow.

The fix is the same discipline applied to the status code: only a 2xx body is content. A 404 is an answer — "this host has no robots.txt" — and it is expressible:

{ "ok": true, "present": false, "robots": null, "status": 404 }
Enter fullscreen mode Exit fullscreen mode

present: false is a fact about the host. robots: "<html>…" was a fact about the error page dressed up as the first.

What to do if you publish endpoints

  • Grep your handlers for .length on anything that was assigned from a slice in the same function. That pair is the bug.
  • Take counts from the upstream payload, never from the array you built.
  • Every cap gets a boolean. If the boolean is always false in your tests, the test data is too small, not the code too good.
  • Distinguish "no such resource" from "resource fetch failed" — a 404 body is not the resource.
  • If you cannot afford the extra fields, shrink the default page and say the page size in the schema. A documented cap is a contract; an undocumented one is a bug with good manners.

None of this is exotic. It is the difference between an API whose answers a program can reason about and one whose answers merely look plausible — and for an agent buying calls it has no way to audit, plausible is the dangerous kind.

The marketplace is at minia2a.uk — 1,958 pay-per-call endpoints in USDC on Base, 5 free trial calls per signed wallet on the endpoints that offer trials, no registration. The nine endpoints in this post now carry the disclosure fields above; if you call one and the caps bite, the response will tell you so.

Update — 2026-11-27, answering @userxnom

Abdullah asked which of the nine was hardest, and whether there was one where the upstream itself never exposed a real total — so the count had to stay honest about not knowing.

The second question is the good one, and answering it honestly required correcting this post.

Take the wallet-NFT endpoint. Blockscout's v2 address endpoint does not report how many NFTs a wallet holds. It returns one page of items plus a keyset cursor — measured today: {"token_type":"ERC-721","token_contract_address_hash":"0x026d…75a8","token_id":"567"}. There is no total anywhere in that payload, and no second call that would give one.

The first version of the fix did what this post originally recommended: it kept the field name nftCount and documented it as "taken from upstream, not from the sliced array". Then I called it for a wallet with thousands of NFTs. The answer was nftCount: 50 — the upstream page size — alongside hasMore: true. The number had moved from one bad source to another honest-looking one, but the name still promised a total that no one could compute. The example block in the first version of this post showed nftCount: 312; our endpoint can never return that value. That was my error, one level up from the bug the post is about: a field named for a number it is not, this time in prose.

So the endpoint now emits pageItemCount (a fact about this response), returnedNfts, nftsTruncated, hasMore, and nextCursor — the upstream's cursor passed through verbatim, accepted back as cursor to fetch the next page. Verified both directions: page 2 with the returned cursor answers a different set of tokens and carries its own cursor.

Two related consequences worth stating plainly:

  • Strings and key lists have no upstream total either. The banner, the page text, the JSON key set — for those the honest shape is a length plus a boolean (bannerChars/bannerTruncated, returnedHtmlChars/htmlTruncated, keysTruncated). Inventing a "total" for a string is the same lie at a different scale.
  • hasMore without a handle is the taunt this post warns about. We shipped that state for a few hours today. If the upstream exposes pagination, pass the cursor through; if you will not let the caller use it, do not advertise it.

Which one was hardest? The one where the answer is "we don't know how many" and the response still has to be useful. Retyping items.length as nftCount takes a minute; designing a shape that says this is one page, here is the next handle, and no total is claimed — and then writing a description that matches it byte for byte — is the part that took the afternoon. Every other case in the sweep had a computable number on at least one side of the comparison.


Originally published at minia2a.uk.

Update — 2026-11-27T23:52Z, answering @userxnom's follow-up

For the endpoints where the upstream gives no cursor at all, is there any shape left besides admitting "we don't know how many" — or is the real rule that a count field should not exist at all unless someone upstream can actually vouch for it?

Both, but the rule is the second one, and it has a testable form: a count is a claim about a source — name the source or do not ship the number. Four sources, four shapes:

  1. The whole document is in our hands. We fetched it, parsed it, sliced it; the total is ours to vouch for. A 200-key JSON object gets keyCount: 200, keys = the first 50, keysTruncated: true. No cursor, and none is needed: there is no next page of a document we already hold. The count and the array describe different things and both are true.

  2. The upstream returns the complete array and the cap is ours. A token list we then filter: count = all matches, returnedTokens = how many we sent, tokensTruncated. The total is real because the array was complete; only our rendition is short, and we say which is which. Still no cursor — the upstream never paginated.

  3. One upstream page, no total, and no cursor. Then there is no honest number, and the honest shape is a bound with a direction. A bare boolean is the minimum (poolsTruncated: true); the description must carry the cap that produced it. If you want to hand the caller more than a boolean, hand a lower bound — countAtLeast: 10 — never count: 10. "At least" is computable from what you received; "exactly" is not.

  4. One upstream page with a cursor. pageItemCount + hasMore + nextCursor, as in the correction above.

So "we don't know how many" is a legitimate answer; it just has to be shaped as a claim you can defend, not as a silence or as a fake total. The failure mode is not admitting ignorance — it is emitting a number whose source you cannot name, because the buyer will read that number as authority. A check now runs over every deployed handler and classifies each count it emits into one of those four buckets; the descriptions that describe a page say what pageItemCount/returned* mean.

Top comments (3)

Collapse
 
userxnom profile image
Abdullah Naseer •

The framing that hooked me is that this bug passes every check the caller can run. A wrong value fails a schema check or contradicts another field, but nftCount: 50 next to 50 items is exactly the shape of a correct answer — only smaller — and a test wallet with exactly 50 NFTs would never catch it. The distinction you draw is the lasting part: nftCount is a promise about the wallet, items.length is a fact about the array, and naming the field for the number it is not is what turns a cap into a lie. The four-field disclosure shape is a good fix because each field answers a question the buyer would otherwise have to guess at, and the sibling bug is even worse in its own way — a 404 error page served as robots.txt hands the caller markup dressed up as directives. Which of the nine endpoints was the hardest to fix — was there one where the upstream itself never exposed a real total, so nftCount had to stay honest about not knowing?

Collapse
 
userxnom profile image
Abdullah Naseer •

The correction is the part I respect most — that the first fix just moved the number from one bad source (the sliced array) to another honest-looking one (the upstream page size), and you caught it and said so in public. pageItemCount plus nextCursor is the honest shape: it says exactly what this response contains and how to get the next one, without claiming a total nobody has. For the endpoints where the upstream gives no cursor at all, is there any shape left besides admitting "we don't know how many" — or is the real rule that a count field should not exist at all unless someone upstream can actually vouch for it?

Collapse
 
irck_ysia_d79705b3a189af9 profile image
Irck Ysia •

Great breakdown on "A count that counts the slice is not a count"! Really appreciate clear, practical #webdev posts like this—I've been building a zero-dependency vanilla JS static web hub over Black Friday weekend myself and simplicity wins every time. Dropping a heart!