Skip to content

fix(knowledge): cache the ingestion usage gate for a minute - #7725

Merged
waleedlatif1 merged 1 commit into
stagingfrom
fix/cached-ingestion-usage-gate
Sep 10, 2026
Merged

waleedlatif1 merged 1 commit into
stagingfrom
fix/cached-ingestion-usage-gate

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Background document processing checked the payer's spend on every document by summing the usage ledger for the billing period.
  • Ingestion now reads the gate through a per-process lru-cache keyed by payer, billing period and acting member, 60 s TTL, misses coalesced with coalesceLocally. Staleness is bounded in the safe direction: a payer at the limit indexes for at most another minute; a raised limit applies within a minute
  • Interactive callers (uploads, search, settings) keep calling checkAttributedUsageLimits fresh, following the isOrganizationBYOKEntitledCached precedent

Type of Change

  • Bug fix

Testing

  • New ingestion-usage-gate.test.ts: one ledger read per payer/period/actor within the TTL, concurrent misses coalesced, refusals cached and re-read after expiry, distinct keys per actor/period/payer, failed reads not cached
  • Existing document-processing tests reset the cache per test; lib/knowledge/documents, lib/billing/core, background/knowledge-processing: 746 passing
  • bun run type-check, bun run lint, bun run check:audits, docs-manifest:check pass

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

🤖 Generated with Claude Code

https://claude.ai/code/session_01JBacX6HGVhPMUySuMfANwn

Every document run checked the payer's spend by summing the usage ledger
for the billing period. The ledger grows with every indexed document, so a
bulk connector sync re-read the whole period's ledger once per document:
41,000 sums over roughly 436,000 rows each during one crawl, which was the
largest single database consumer while it ran and slowed unrelated queries,
including search.

Background ingestion now reads the gate through a per-process LRU cache
keyed by payer, billing period and acting member, with a 60-second TTL and
coalesced misses. Staleness fails in the harmless direction: a payer at
their limit indexes for at most another minute, and a raised limit takes at
most a minute to apply. Interactive callers keep reading the gate fresh.

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

vercel Bot commented Sep 10, 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 10, 2026 9:14am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with the bounded usage-gate staleness matching its documented performance tradeoff.

Summary

  • Keys cached decisions by payer, billing-period bounds, and acting member.
  • Coalesces concurrent cache misses through the existing singleflight utility.
  • Caches both allowed and refused decisions while leaving interactive usage checks unchanged.
  • Adds focused coverage for TTL behavior, key separation, concurrent misses, failures, and test isolation.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    D[Background document processing] --> G[checkIngestionUsageLimits]
    G --> C{Cached decision exists?}
    C -->|Yes, within 60 seconds| R[Return cached decision]
    C -->|No| S[Coalesce concurrent misses]
    S --> L[Read attributed usage limits]
    L --> W[Cache result for 60 seconds]
    W --> R
    R --> E{Usage exceeded?}
    E -->|Yes| X[Skip indexing]
    E -->|No| I[Index document and record usage]
Loading

Reviews (1) · Last reviewed commit: "fix(knowledge): cache the ingestion usag..."

@waleedlatif1
waleedlatif1 merged commit 6d42e7d into staging Sep 10, 2026
34 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/cached-ingestion-usage-gate branch September 10, 2026 09:23
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