#2551·OpenCLI

fix(zhihu): answer-comments pagination breaks when server rewrites next-page limit to 10

Author: xcpumaCreated Sep 26, 2026Updated Sep 26, 2026

Environment

  • opencli 1.8.8 (npm global), Windows 11, Node v22.14.0

Symptom

zhihu answer-comments fails whenever the result set spans more than one page (--limit > single page of root comments):

bash
opencli zhihu answer-comments 2087211197766561930 --limit 30 -f json
ok: false
error:
  code: COMMAND_EXEC
  message: Zhihu answer root comments pagination returned a malformed next URL

--limit values that fit on the first page work fine, which makes the bug look intermittent but it is deterministic.

Root cause

The comment_v5 API now rewrites the limit param in paging.next to its own default (10), regardless of the limit=20 the adapter requested. Live response from GET /api/v4/comment_v5/answers/<aid>/root_comment?order_by=score&limit=20&offset= (fetched with browser credentials):

json
{
  "paging": {
    "is_end": false,
    "next": "https://www.zhihu.com/api/v4/comment_v5/answers/<aid>/root_comment?limit=10&offset=&order_by=score"
  }
}

In clis/zhihu/answer-comments-helpers.js, normalizePageUrl() validates the next URL against limit: String(PAGE_SIZE) ('20'). Since the server now returns limit=10, validation returns '' and fetchPages() throws malformed next URL.

Same strict check exists for child comments (fetchChildComments).

Proposed fix

Relax limit to an existence check (the server owns its default page size; host/path allowlisting, order_by exact match, and the visited-URL cycle guard are unchanged), and budget maxPages by the observed server page size so large --limit values don't hit exceeded its fetch budget early:

diff
--- a/clis/zhihu/answer-comments-helpers.js
+++ b/clis/zhihu/answer-comments-helpers.js
@@ fetchPages()
-    const maxPages = Math.ceil(limit / PAGE_SIZE) + PAGE_OVERLAP_ALLOWANCE;
+    // Zhihu rewrites the next-page `limit` to its own default (10), so budget by the smaller page size.
+    const maxPages = Math.ceil(limit / 10) + PAGE_OVERLAP_ALLOWANCE;

@@ fetchRootComments()
         normalizeNext: (next) => normalizePageUrl(next, path, {
             order_by: apiOrder,
-            limit: String(PAGE_SIZE),
+            limit: null,
             offset: null,
         }),

@@ fetchChildComments()
-        normalizeNext: (next) => normalizePageUrl(next, path, { limit: String(PAGE_SIZE), offset: null }),
+        normalizeNext: (next) => normalizePageUrl(next, path, { limit: null, offset: null }),

Verification

Patched locally and re-ran the repro:

$ opencli zhihu answer-comments 2087211197766561930 --limit 30 -f json
→ 51 rows returned (root comments + replies, pagination crossed pages), no error

Before the patch the same command failed 100% of the time; --limit 20 capped results at the first page.