Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(notion): 递归子块分页补重复游标与跳数上限,兜底不再被空页绕过 (#4411)
linus-liu-web26ff9888238d
diff --git a/internal/datasource/connector/notion/client.go b/internal/datasource/connector/notion/client.goindex 86c16819a..11a720382 100644--- a/internal/datasource/connector/notion/client.go+++ b/internal/datasource/connector/notion/client.go@@ -333,8 +333,9 @@ func (c *notionClient) GetBlockChildrenAll(ctx context.Context, blockID string)func (c *notionClient) getBlockChildrenRecursive(ctx context.Context, blockID string, depth int) ([]notionBlock, error) {var allBlocks []notionBlockvar startCursor string+ seenCursors := make(map[string]struct{})- for {+ for page := 1; ; page++ {path := fmt.Sprintf("/v1/blocks/%s/children", blockID)if startCursor != "" {path += "?start_cursor=" + startCursor@@ -376,7 +377,14 @@ func (c *notionClient) getBlockChildrenRecursive(ctx context.Context, blockID stif len(allBlocks) >= maxBlocksPerPage || !resp.HasMore || resp.NextCursor == "" {break}- startCursor = resp.NextCursor+ // maxBlocksPerPage counts appended blocks, so an empty page carrying+ // has_more=true never reaches it. Guard the cursor itself as well: a+ // repeated one would otherwise be requested forever.+ next, err := advancePaginationCursor(ctx, seenCursors, resp.NextCursor, page)+ if err != nil {+ return nil, fmt.Errorf("get block children for %s: %w", blockID, err)+ }+ startCursor = next}if depth >= maxBlockDepth {diff --git a/internal/datasource/connector/notion/pagination_guard_test.go b/internal/datasource/connector/notion/pagination_guard_test.goindex a2d5d5c8a..729234fbe 100644--- a/internal/datasource/connector/notion/pagination_guard_test.go+++ b/internal/datasource/connector/notion/pagination_guard_test.go@@ -148,6 +148,82 @@ func TestGetBlockChildrenFlatRejectsRepeatedCursor(t *testing.T) {require.Contains(t, err.Error(), `repeated next_cursor "same-cursor"`)}+// GetBlockChildrenAll is the recursive sibling of GetBlockChildrenFlat and+// carried the same unbounded cursor loop. Its own maxBlocksPerPage fallback+// counts blocks already appended, so a vendor answering empty results with+// has_more=true keeps that counter at zero forever and the loop never exits.+func TestGetBlockChildrenAllRejectsRepeatedCursor(t *testing.T) {+ var requests atomic.Int64+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ if requests.Add(1) > fakeLoopBudget {+ w.WriteHeader(http.StatusBadRequest)+ return+ }+ writeListPage(w, `[]`, true, "same-cursor")+ }))+ defer server.Close()++ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)+ defer cancel()++ _, err := paginationTestClient(t, server).GetBlockChildrenAll(ctx, "block-1")+ require.Error(t, err)+ require.Equal(t, int64(2), requests.Load(),+ "the guard must reject the repeated cursor on the second page; err=%v", err)+ require.Contains(t, err.Error(), `repeated next_cursor "same-cursor"`)+}++// A vendor that mints a fresh cursor for every empty page never repeats one, so+// only the hop cap can end the recursive walk.+func TestGetBlockChildrenAllStopsAtPaginationCap(t *testing.T) {+ var requests atomic.Int64+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ n := requests.Add(1)+ if n > fakeLoopBudget {+ w.WriteHeader(http.StatusBadRequest)+ return+ }+ writeListPage(w, `[]`, true, fmt.Sprintf("cursor-%d", n))+ }))+ defer server.Close()++ ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)+ defer cancel()++ _, err := paginationTestClient(t, server).GetBlockChildrenAll(ctx, "block-1")+ require.Error(t, err)+ require.Contains(t, err.Error(), "pagination exceeded 10000 pages")+ require.Equal(t, int64(paginationHopCap), requests.Load())+}++// The recursive walk must still merge a genuinely multi-page listing.+func TestGetBlockChildrenAllMergesRealPages(t *testing.T) {+ var requests atomic.Int64+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ requests.Add(1)+ if r.URL.Query().Get("start_cursor") == "" {+ writeListPage(w, `[{"id":"block-1","type":"paragraph",`++ `"paragraph":{"rich_text":[{"type":"text","text":{"content":"one"},"plain_text":"one"}]}}]`,+ true, "cursor-2")+ return+ }+ writeListPage(w, `[{"id":"block-2","type":"paragraph",`++ `"paragraph":{"rich_text":[{"type":"text","text":{"content":"two"},"plain_text":"two"}]}}]`,+ false, "")+ }))+ defer server.Close()++ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)+ defer cancel()++ blocks, err := paginationTestClient(t, server).GetBlockChildrenAll(ctx, "block-1")+ require.NoError(t, err)+ require.Len(t, blocks, 2)+ require.Equal(t, "block-1", blocks[0].ID)+ require.Equal(t, "block-2", blocks[1].ID)+ require.Equal(t, int64(2), requests.Load())+}+// Data source queries page through queryDataSourceWindow since #3845, so the// repeated-cursor guard has to cover that loop too.func TestQueryDataSourceAllRejectsRepeatedCursor(t *testing.T) {