fix(notion): 超过 block 上限截断时补 Warn,避免内容与子页面静默缺失 (#3771)

* fix(notion): 超过 block 上限截断时补 Warn,避免内容与子页面静默缺失

单个页面/块超过 maxBlocksPerPage(1000) 时客户端会静默停止分页:第 1000
个 block 之后的正文不会进入 markdown,其后的 child_page/child_database
也不会被 fetchPage 递归同步。又因为游标记录的是 last_edited_time,页面
未变更时同步会直接跳过该页,截断因此长期存在且无任何提示。飞书同类上限
(feishu/core/blocks.go 的 maxDocumentBlocks)已有 Warn,这里对齐其措辞
补齐可观测性。

- cap 触发且 API 仍有下一页时打一条 Warn,含块/页面 ID 与上限数字,说明
  剩余 block 不会被同步;是否真为截断由纯函数 blocksTruncated 判定,恰好
  1000 个 block 的页面不会误报;
- 不改 cap 数值、page_size、递归逻辑与游标语义。

测试 truncation_test.go:伪造返回 1001 个 block 的 children 接口,断言
Warn 内容且第 11 次请求未发出;另覆盖恰好 1000 的边界与 blocksTruncated
真值表。

* fix(notion): has_more 但 cursor 为空时不再静默丢内容

blocksTruncated 的第三个条件 nextCursor != "" 在 Notion 契约下与 has_more
冗余:has_more=false 时 next_cursor 必为 null。于是"has_more=true 但
next_cursor 为空"这种异常响应仍会静默截断——未命中 cap 时循环本就因空游标
break,命中 cap 时又因该条件跳过 Warn,两种情况都不留痕迹。

- 谓词收敛为 currentCount >= maxBlocksPerPage && hasMore,不再要求游标非空:
  cap 命中且 has_more=true 即视为截断;
- has_more=true 而 next_cursor 为空时单独打一条 Warn,带上块 ID 与已取块数,
  说明无法继续翻页、剩余内容不会被同步(无论是否命中 cap 都打);
- 不改 cap 数值、break 条件、page_size、递归深度与游标语义。

测试:真值表去掉游标列,并删掉把"cap 命中 + 空游标 = 不告警"固化成期望的
用例;新增 TestBlockCapWarnsWhenHasMoreHasNoCursor(cap 命中 + has_more=true
+ 空游标须同时出现 cap Warn 与游标 Warn,且不发出第 11 次请求)与
TestHasMoreWithoutCursorWarnsBelowTheCap(cap 以下同样要告警);原有正常翻页
用例补断言:格式正确的分页不得出现游标异常 Warn。反向验证:谓词还原为原样
后两个新用例均失败。

* test(notion): rename block-cap log helper to avoid clashing with #3845

---------

Co-authored-by: linus-liu-web <11640009+linus-liu-web@users.noreply.github.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
main
linus-liu-web 2026-10-08 01:29:12 +08:00 committed by GitHub
parent 6057a81017
commit 5077f35800
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 300 additions and 0 deletions

View File

@ -274,6 +274,19 @@ func (c *notionClient) GetBlockChildrenFlat(ctx context.Context, blockID string)
const maxBlockDepth = 5 // Limit recursion depth — deeper content has diminishing value for knowledge bases
const maxBlocksPerPage = 1000 // Limit total blocks fetched per page to prevent runaway API calls
// blocksTruncated reports whether the maxBlocksPerPage cap stopped pagination
// while the API still offered another page — i.e. whether content was actually
// dropped. Hitting the cap on the last page of a document is not truncation,
// so the caller can warn without a false positive.
//
// hasMore alone decides that: the Notion contract only clears it together with
// next_cursor, so requiring a non-empty cursor here would let the anomalous
// "has_more=true, next_cursor empty" response drop content in silence. That
// anomaly is reported separately by the caller.
func blocksTruncated(currentCount int, hasMore bool) bool {
return currentCount >= maxBlocksPerPage && hasMore
}
// GetBlockChildrenAll recursively fetches all blocks under a given block ID,
// building a tree structure with Children populated for blocks with has_children=true.
// child_page and child_database blocks are NOT recursed into (handled by connector layer).
@ -309,6 +322,22 @@ func (c *notionClient) getBlockChildrenRecursive(ctx context.Context, blockID st
allBlocks = append(allBlocks, blocks...)
// The cap below stops pagination unconditionally; without this warning
// the dropped blocks (and any child_page/child_database they contain,
// which the connector layer never visits) vanish silently.
if blocksTruncated(len(allBlocks), resp.HasMore) {
logger.Warnf(ctx, "[Notion] block %s exceeded %d blocks; truncating, remaining blocks are not synced",
blockID, maxBlocksPerPage)
}
// has_more promises another page, but an empty next_cursor means there is
// no way to ask for it: pagination stops right here even below the cap,
// so everything after this page would be dropped without a trace.
if resp.HasMore && resp.NextCursor == "" {
logger.Warnf(ctx, "[Notion] block %s returned has_more=true without a next_cursor after %d blocks; "+
"cannot fetch further pages, remaining blocks are not synced", blockID, len(allBlocks))
}
if len(allBlocks) >= maxBlocksPerPage || !resp.HasMore || resp.NextCursor == "" {
break
}

View File

@ -0,0 +1,271 @@
package notion
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync/atomic"
"testing"
"github.com/Tencent/WeKnora/internal/logger"
)
// childrenPageFunc decides, for the page covering [offset, end), whether the
// response claims another page and which cursor it carries.
type childrenPageFunc func(offset, end int) (hasMore bool, nextCursor interface{})
// fakeChildrenPages serves GET /v1/blocks/{blockID}/children in pages of
// pageSize blocks, emulating Notion's pagination contract. The client does not
// pass page_size, so the real API defaults to 100 — the same default used here.
// It returns the number of children requests it saw via the second result.
func fakeChildrenPages(t *testing.T, blockID string, total, pageSize int) (*httptest.Server, *int32) {
t.Helper()
return fakeChildrenPagesShape(t, blockID, total, pageSize, func(_, end int) (bool, interface{}) {
if end < total {
return true, fmt.Sprintf("cursor-%d", end)
}
return false, nil
})
}
// fakeChildrenPagesShape is fakeChildrenPages with per-page control over
// has_more/next_cursor, so a test can reproduce responses the documented
// contract does not promise (e.g. has_more=true with an empty cursor).
func fakeChildrenPagesShape(t *testing.T, blockID string, total, pageSize int,
page childrenPageFunc,
) (*httptest.Server, *int32) {
t.Helper()
var requests int32
mux := http.NewServeMux()
mux.HandleFunc("/v1/blocks/"+blockID+"/children", func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&requests, 1)
offset := 0
if cursor := r.URL.Query().Get("start_cursor"); cursor != "" {
n, err := strconv.Atoi(strings.TrimPrefix(cursor, "cursor-"))
if err != nil {
http.Error(w, "malformed start_cursor: "+cursor, http.StatusBadRequest)
return
}
offset = n
}
end := offset + pageSize
if end > total {
end = total
}
results := make([]map[string]interface{}, 0, end-offset)
for i := offset; i < end; i++ {
results = append(results, map[string]interface{}{
"object": "block",
"id": fmt.Sprintf("blk-%d", i+1),
"type": "paragraph",
"has_children": false,
"paragraph": map[string]interface{}{"rich_text": []interface{}{}},
})
}
hasMore, nextCursor := page(offset, end)
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"object": "list",
"results": results,
"has_more": hasMore,
"next_cursor": nextCursor,
})
})
ts := httptest.NewServer(mux)
t.Cleanup(ts.Close)
return ts, &requests
}
// captureBlockCapLogs redirects the project logger into a buffer for the duration
// of the test. ConfigureFromEnv in cleanup restores output and level.
func captureBlockCapLogs(t *testing.T) *bytes.Buffer {
t.Helper()
var buf bytes.Buffer
logger.SetLogLevel(logger.LevelDebug)
logger.SetOutput(&buf)
t.Cleanup(func() { logger.ConfigureFromEnv() })
return &buf
}
// TestBlockCapWarnsOnTruncation drives the real paging loop against a fake
// children endpoint holding 1001 blocks. The cap must still stop at 1000
// (behavior unchanged), the 1001st block must never be requested, and the
// truncation must be reported instead of happening silently.
func TestBlockCapWarnsOnTruncation(t *testing.T) {
ts, requests := fakeChildrenPages(t, "big-page", 1001, 100)
buf := captureBlockCapLogs(t)
client := mustTestClient(t, "test-token", ts.URL)
blocks, err := client.GetBlockChildrenAll(context.Background(), "big-page")
if err != nil {
t.Fatalf("GetBlockChildrenAll() error: %v", err)
}
if len(blocks) != maxBlocksPerPage {
t.Fatalf("blocks = %d, want %d (the cap itself must not change)", len(blocks), maxBlocksPerPage)
}
if got := atomic.LoadInt32(requests); got != 10 {
t.Errorf("children requests = %d, want 10 (no page may be requested past the cap)", got)
}
out := buf.String()
if !strings.Contains(out, "WARN") {
t.Fatalf("expected a Warn log entry, got:\n%s", out)
}
for _, want := range []string{"[Notion]", "big-page", "exceeded 1000 blocks", "truncating"} {
if !strings.Contains(out, want) {
t.Errorf("truncation warning missing %q, got:\n%s", want, out)
}
}
if strings.Contains(out, "without a next_cursor") {
t.Errorf("well-formed pagination must not trigger the cursor warning, got:\n%s", out)
}
}
// TestBlockCapWarnsWhenHasMoreHasNoCursor covers the response the documented
// contract does not promise: the page that reaches the cap claims has_more=true
// but carries no next_cursor, so pagination cannot continue. Both facts must be
// reported — the cap hit and the unusable cursor. This is the exact cell that
// used to be silent: the old predicate required a non-empty cursor, so the cap
// warning was skipped and the dropped tail left no trace at all.
func TestBlockCapWarnsWhenHasMoreHasNoCursor(t *testing.T) {
const total = maxBlocksPerPage + 1
ts, requests := fakeChildrenPagesShape(t, "odd-page", total, 100, func(_, end int) (bool, interface{}) {
if end == maxBlocksPerPage {
return true, nil // has_more=true with no usable cursor
}
if end < total {
return true, fmt.Sprintf("cursor-%d", end)
}
return false, nil
})
buf := captureBlockCapLogs(t)
client := mustTestClient(t, "test-token", ts.URL)
blocks, err := client.GetBlockChildrenAll(context.Background(), "odd-page")
if err != nil {
t.Fatalf("GetBlockChildrenAll() error: %v", err)
}
if len(blocks) != maxBlocksPerPage {
t.Fatalf("blocks = %d, want %d (the cap itself must not change)", len(blocks), maxBlocksPerPage)
}
if got := atomic.LoadInt32(requests); got != 10 {
t.Errorf("children requests = %d, want 10 (an empty cursor must stop pagination)", got)
}
out := buf.String()
if !strings.Contains(out, "WARN") {
t.Fatalf("expected a Warn log entry, got:\n%s", out)
}
for _, want := range []string{
"[Notion]", "odd-page", "exceeded 1000 blocks",
"has_more=true", "without a next_cursor", "remaining blocks are not synced",
} {
if !strings.Contains(out, want) {
t.Errorf("warning missing %q, got:\n%s", want, out)
}
}
}
// TestHasMoreWithoutCursorWarnsBelowTheCap pins the other half of the anomaly:
// has_more=true with an empty cursor stops pagination no matter how far below
// the cap the client is, so a dropped tail must be reported there as well — the
// cap warning is not what makes this case observable.
func TestHasMoreWithoutCursorWarnsBelowTheCap(t *testing.T) {
const total = 500
ts, requests := fakeChildrenPagesShape(t, "odd-small-page", total, 100, func(_, end int) (bool, interface{}) {
if end == 300 {
return true, nil // has_more=true with no usable cursor
}
if end < total {
return true, fmt.Sprintf("cursor-%d", end)
}
return false, nil
})
buf := captureBlockCapLogs(t)
client := mustTestClient(t, "test-token", ts.URL)
blocks, err := client.GetBlockChildrenAll(context.Background(), "odd-small-page")
if err != nil {
t.Fatalf("GetBlockChildrenAll() error: %v", err)
}
if len(blocks) != 300 {
t.Fatalf("blocks = %d, want 300 (pagination stops at the cursorless page)", len(blocks))
}
if got := atomic.LoadInt32(requests); got != 3 {
t.Errorf("children requests = %d, want 3 (no page may be requested without a cursor)", got)
}
out := buf.String()
if !strings.Contains(out, "WARN") {
t.Fatalf("expected a Warn log entry, got:\n%s", out)
}
for _, want := range []string{
"[Notion]", "odd-small-page", "has_more=true", "without a next_cursor",
"after 300 blocks", "remaining blocks are not synced",
} {
if !strings.Contains(out, want) {
t.Errorf("cursor warning missing %q, got:\n%s", want, out)
}
}
if strings.Contains(out, "exceeded 1000 blocks") {
t.Errorf("below the cap there is no truncation to report, got:\n%s", out)
}
}
// TestBlockCapExactlyAtLimitDoesNotWarn guards the off-by-one at the cap: a
// document whose last page ends exactly at maxBlocksPerPage has dropped
// nothing, so no truncation warning may be emitted.
func TestBlockCapExactlyAtLimitDoesNotWarn(t *testing.T) {
ts, _ := fakeChildrenPages(t, "exact-page", maxBlocksPerPage, 100)
buf := captureBlockCapLogs(t)
client := mustTestClient(t, "test-token", ts.URL)
blocks, err := client.GetBlockChildrenAll(context.Background(), "exact-page")
if err != nil {
t.Fatalf("GetBlockChildrenAll() error: %v", err)
}
if len(blocks) != maxBlocksPerPage {
t.Fatalf("blocks = %d, want %d", len(blocks), maxBlocksPerPage)
}
if out := buf.String(); strings.Contains(out, "truncating") {
t.Errorf("exactly %d blocks is not truncation, got:\n%s", maxBlocksPerPage, out)
}
}
func TestBlocksTruncated(t *testing.T) {
// has_more is the only signal consulted: "cap hit while has_more=true but
// next_cursor empty" is no longer classified as "not truncated" here — the
// paging loop warns about it unconditionally (see
// TestBlockCapWarnsWhenHasMoreHasNoCursor and
// TestHasMoreWithoutCursorWarnsBelowTheCap).
cases := []struct {
name string
currentCount int
hasMore bool
want bool
}{
{"cap reached with a next page", 1000, true, true},
{"cap overshot by a partial page", 1050, true, true},
{"cap reached on the final page", 1000, false, false},
{"cap overshot on the final page", 1050, false, false},
{"below the cap", 999, true, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := blocksTruncated(tc.currentCount, tc.hasMore); got != tc.want {
t.Errorf("blocksTruncated(%d, %v) = %v, want %v",
tc.currentCount, tc.hasMore, got, tc.want)
}
})
}
}