Skip to content

fix(knowledge): count list documents once instead of per 400-base batch - #7759

Merged
waleedlatif1 merged 1 commit into
stagingfrom
fix/knowledge-list-live-source-counts
Sep 11, 2026
Merged

fix(knowledge): count list documents once instead of per 400-base batch#7759
waleedlatif1 merged 1 commit into
stagingfrom
fix/knowledge-list-live-source-counts

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

Staging alarm sim-staging-us-east-1-integ-failure (2026-09-10/11): integ-session-kb-document-lifecycle/archive-kb hits its 30 s client timeout on GET /api/knowledge?scope=archived. The request completes with 200 server-side but takes 90 to 122 s.

Root cause. #7730 switched the knowledge base lists to pass a KnowledgeAccessProvider, which flipped readKnowledgeBaseRows onto a new path: the document join became sql\false`andreadKnowledgeBaseDocumentCountsran a count query plusknowledgeReadAccessBatches` candidate discovery per batch of 400 bases. The integ workspace holds 21,763 archived bases (fixtures archived since June, never purged), so one unpaged list became ~55 batches / 110+ sequential queries. Integ log shows archive-kb at 3 to 5 s until 11:02 UTC and 30 s timeouts from 11:29 UTC, 14 minutes after #7730 merged.

Changes

  • readKnowledgeBaseRows resolves the scope once and counts stored-ACL documents in the list's own join again, as before fix(search): verify Atlassian access and clarify source setup #7730.
  • Only documents a live source (GitHub, Confluence) authorizes beyond the stored ACL are counted afterwards, once for the whole list. The supplement is bounded by the list filter for an unpaged list, or by the page's ids for a paged one, so it never scales with base count.
  • KnowledgeAccessProvider gains an optional hasLiveSourceReaders(). knowledgeReadAccessBatches skips candidate discovery when it returns false: without a live-source credential getForConnectors returns the ordinary scope, so the discovered predicate is NOT ordinary AND ordinary and provably empty. This applies to every generator call site (documents, tags, files, connectors), not just the list.
  • attachKnowledgeBaseConnectors uses the same two helpers for the single-base path.

Semantics are unchanged: for a reader with live-source credentials, ordinary plus live counts sum exactly as #7730 intended; for everyone else the result is the pre-#7730 single query.

Verification

  • bun run type-check, bun run check:audits: pass.
  • vitest run lib/knowledge app/api/knowledge app/api/files app/api/v1/knowledge app/api/v2/knowledge: 202 files, 3033 tests pass.
  • New tests (unpaged list counts in one join + one discovery pass bounded by the filter; no discovery without live readers; generator short-circuit; provider reports readers) all fail with the fix reverted.
  • Not done: the fixture graveyard itself. The remaining ~4 s on that endpoint is the 21k-row unpaged payload, which data retention will address.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Lm5mdDU9bi1vb4oAvPtbjn

#7730 switched the knowledge base lists to a KnowledgeAccessProvider, which
turned the single joined count into a per-400-base loop of count plus
candidate-discovery queries. The staging integ workspace holds 21k archived
bases, so GET /api/knowledge?scope=archived went from ~4s to ~100s and the
archive-kb integ check timed out at 30s.

The stored-ACL count moves back into the list's own join. Only documents a
live source (GitHub, Confluence) authorizes beyond that are counted
afterwards, once for the whole list, bounded by the list filter (unpaged) or
the page's ids. The provider now reports whether the reader holds any
live-source credential, and the shared batch generator skips candidate
discovery entirely when it does not, since the discovered predicate would be
provably empty.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lm5mdDU9bi1vb4oAvPtbjn
@vercel

vercel Bot commented Sep 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Sep 11, 2026 2:36am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge with the optimized query flow preserving existing access and counting behavior.

Summary

  • Resolves the ordinary access scope once and counts stored-ACL-visible documents in the list query.
  • Performs live-source candidate discovery once against the page IDs or unpaged list filter.
  • Skips candidate discovery when the reader has no GitHub or Confluence credential capable of granting additional access.
  • Applies the same ordinary-plus-live counting model to single-base connector attachment.
  • Adds focused tests for additive counts, unpaged lists, and the no-live-credential fast path.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[List knowledge bases] --> B[Resolve ordinary access scope once]
    B --> C[List query counts stored-ACL documents]
    C --> D{Access provider and returned rows?}
    D -->|No| G[Return ordinary totals]
    D -->|Yes| E{Live-source readers exist?}
    E -->|No| G
    E -->|Yes| F[Discover and count only live-authorized documents outside ordinary access]
    F --> H[Add disjoint live totals to ordinary totals]
    H --> I[Return knowledge bases with counts]
Loading

Reviews (1) · Last reviewed commit: "fix(knowledge): count list documents onc..."

@waleedlatif1
waleedlatif1 merged commit b70820b into staging Sep 11, 2026
33 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/knowledge-list-live-source-counts branch September 11, 2026 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant