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(cli): apply list limits after filtering and sorting (#3990)
LeonSGPcefd5920f885
diff --git a/cli/cmd/doc/list.go b/cli/cmd/doc/list.goindex 4ac9d6163..c72b890c0 100644--- a/cli/cmd/doc/list.go+++ b/cli/cmd/doc/list.go@@ -177,6 +177,7 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOption// non-walking path returns the first page only.var items []sdk.Knowledgevar serverTotal int64+ truncated := falseif opts.AllPages {accum := make([]sdk.Knowledge, 0)for page := 1; ; page++ {@@ -187,7 +188,7 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOptionserverTotal = totalaccum = append(accum, chunk...)if opts.Limit > 0 && len(accum) >= opts.Limit {- accum = accum[:opts.Limit]+ truncated = int64(len(accum)) < totalbreak}if int64(len(accum)) >= total || len(chunk) == 0 {@@ -214,7 +215,6 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOption})// --limit applies after sort so users get the top-N most-recent items// when combined with a single-page fetch where page_size > limit.- truncated := falseif opts.Limit > 0 && len(items) > opts.Limit {items = items[:opts.Limit]truncated = truediff --git a/cli/cmd/doc/list_limit_order_test.go b/cli/cmd/doc/list_limit_order_test.gonew file mode 100644index 000000000..c548db7d6--- /dev/null+++ b/cli/cmd/doc/list_limit_order_test.go@@ -0,0 +1,44 @@+package doc++import (+ "context"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "testing"+ "time"++ "github.com/Tencent/WeKnora/cli/internal/cmdutil"+ "github.com/Tencent/WeKnora/cli/internal/iostreams"+ sdk "github.com/Tencent/WeKnora/client"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++func TestListAllPagesSortsBeforeLimit(t *testing.T) {+ out, _ := iostreams.SetForTest(t)+ now := time.Now()+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ assert.Equal(t, "/api/v1/knowledge-bases/kb-test/knowledge", r.URL.Path)+ _ = json.NewEncoder(w).Encode(sdk.KnowledgeListResponse{+ Success: true, Total: 2,+ Data: []sdk.Knowledge{+ {ID: "older", UpdatedAt: now.Add(-time.Hour)},+ {ID: "newer", UpdatedAt: now},+ },+ })+ }))+ defer server.Close()+ require.NoError(t, runList(context.Background(), &ListOptions{PageSize: 2, Limit: 1, AllPages: true},+ &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, sdk.NewClient(server.URL), "kb-test"))+ var envelope struct {+ Data []sdk.Knowledge `json:"data"`+ Meta struct {+ HasMore bool `json:"has_more"`+ } `json:"meta"`+ }+ require.NoError(t, json.Unmarshal(out.Bytes(), &envelope))+ require.Len(t, envelope.Data, 1)+ assert.Equal(t, "newer", envelope.Data[0].ID)+ assert.True(t, envelope.Meta.HasMore)+}diff --git a/cli/cmd/session/list.go b/cli/cmd/session/list.goindex 329deacd3..18255cd4e 100644--- a/cli/cmd/session/list.go+++ b/cli/cmd/session/list.go@@ -115,22 +115,38 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOptionsince = d}+ threshold := time.Now().Add(-since)+ filterSince := func(items []sdk.Session) []sdk.Session {+ if since <= 0 {+ return items+ }+ filtered := items[:0]+ for _, session := range items {+ updated, err := time.Parse(time.RFC3339, session.UpdatedAt)+ if err == nil && updated.After(threshold) {+ filtered = append(filtered, session)+ }+ }+ return filtered+ }+var items []sdk.Sessionvar serverTotal intif opts.AllPages {accum := make([]sdk.Session, 0)+ scanned := 0for page := 1; ; page++ {chunk, total, err := svc.GetSessionsByTenant(ctx, page, opts.PageSize)if err != nil {return cmdutil.WrapHTTP(err, "list sessions")}serverTotal = total- accum = append(accum, chunk...)- if opts.Limit > 0 && len(accum) >= opts.Limit {- accum = accum[:opts.Limit]+ scanned += len(chunk)+ accum = append(accum, filterSince(chunk)...)+ if opts.Limit > 0 && len(accum) > opts.Limit {break}- if len(accum) >= total || len(chunk) == 0 {+ if scanned >= total || len(chunk) == 0 {break}}@@ -141,25 +157,11 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOptionreturn cmdutil.WrapHTTP(err, "list sessions")}serverTotal = total- items = chunk+ items = filterSince(chunk)}if items == nil {items = []sdk.Session{} // JSON [] not null}- if since > 0 {- threshold := time.Now().Add(-since)- filtered := items[:0]- for _, s := range items {- t, err := time.Parse(time.RFC3339, s.UpdatedAt)- if err != nil {- continue // skip unparseable timestamps rather than guess- }- if t.After(threshold) {- filtered = append(filtered, s)- }- }- items = filtered- }// --limit applies after --since so the cap reflects what the user sees.truncated := falseif opts.Limit > 0 && len(items) > opts.Limit {diff --git a/cli/cmd/session/list_filter_limit_test.go b/cli/cmd/session/list_filter_limit_test.gonew file mode 100644index 000000000..04a8bb2d2--- /dev/null+++ b/cli/cmd/session/list_filter_limit_test.go@@ -0,0 +1,53 @@+package sessioncmd++import (+ "context"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "strconv"+ "testing"+ "time"++ "github.com/Tencent/WeKnora/cli/internal/cmdutil"+ "github.com/Tencent/WeKnora/cli/internal/iostreams"+ sdk "github.com/Tencent/WeKnora/client"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++func TestListAllPagesLimitAppliesAfterSince(t *testing.T) {+ out, _ := iostreams.SetForTest(t)+ now := time.Now()+ sessions := []sdk.Session{+ {ID: "old", UpdatedAt: now.Add(-30 * 24 * time.Hour).Format(time.RFC3339)},+ {ID: "recent-one", UpdatedAt: now.Add(-time.Hour).Format(time.RFC3339)},+ {ID: "recent-two", UpdatedAt: now.Add(-2 * time.Hour).Format(time.RFC3339)},+ }+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ page, err := strconv.Atoi(r.URL.Query().Get("page"))+ assert.NoError(t, err)+ assert.Equal(t, "/api/v1/sessions", r.URL.Path)+ assert.Equal(t, "1", r.URL.Query().Get("page_size"))+ data := []sdk.Session{}+ if page >= 1 && page <= len(sessions) {+ data = append(data, sessions[page-1])+ }+ _ = json.NewEncoder(w).Encode(sdk.SessionListResponse{Success: true, Data: data, Total: len(sessions)})+ }))+ defer server.Close()+ require.NoError(t, runList(context.Background(), &ListOptions{PageSize: 1, Limit: 1, AllPages: true, Since: "7d"},+ &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, sdk.NewClient(server.URL)))+ var envelope struct {+ Data []sdk.Session `json:"data"`+ Meta struct {+ HasMore bool `json:"has_more"`+ TotalCount int `json:"total_count"`+ } `json:"meta"`+ }+ require.NoError(t, json.Unmarshal(out.Bytes(), &envelope))+ require.Len(t, envelope.Data, 1)+ assert.Equal(t, "recent-one", envelope.Data[0].ID)+ assert.True(t, envelope.Meta.HasMore)+ assert.Equal(t, 3, envelope.Meta.TotalCount)+}