Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 5 min read

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.

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

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 */ ]
}

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 }

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.

Originally published at minia2a.uk.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.