Skip to content

fix(knowledge): bound every connector-lease ACL transaction to one short page - #8195

Merged
waleedlatif1 merged 10 commits into
stagingfrom
fix/connector-lease-transactions-bounded
Sep 23, 2026
Merged

waleedlatif1 merged 10 commits into
stagingfrom
fix/connector-lease-transactions-bounded

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Every connector-lease transaction that assigns document.acl is one short page: bounded by its documents' chunk rows (PROJECTION_ROW_BATCH_SIZE, the constant detachment already uses, moved to sync-limits; a document larger than the cap gets a page alone), under LEASE_PAGE_LOCK_TIMEOUT_MS (15 s) / LEASE_PAGE_STATEMENT_TIMEOUT_MS (30 s). The ACL trigger rewrites every filled search projection row of a document whose ACL is assigned, so a transaction spanning many batches held the connector row (and the table lock a migration queues behind) across unbounded fan-out, and a timeout rolled back every batch that had landed
  • Lease proved last: leaseTransaction, withMemberLease, the content pass's lease transaction and the stale-member sweep write first and take the connector row lock as their final statement (a lost lease still rolls the page back). No page waits on document rows — which a processing commit may hold for its whole write — while holding a knowledge_connector lock. Only ACL pages carry the page bounds; other lease bodies keep the role's timeouts
  • persistDocumentAcls: the evidence refresh returns what it matched; only the rest is read and written as changes, in row-bounded pages. The unchanged-refresh vs changed-write split and the unresolved-evidence guard are unchanged
  • restoreWorkspaceDocumentAcls: out of the completion transaction, paged, mode-guarded, and run only while accessRewritePending is set (the mode switch and ACL-resetting edits are the only sources of that drift; every other writer of a workspace-mode document writes the workspace ACL)
  • Every ACL page locks its documents and rereads their chunk counts before writing, so a reprocess that lands between planning and writing cannot overfill a page (a processing commit holds its document row while it replaces chunks and sets chunk_count)
  • rewriteConnectorAcls and the restore share one single-pass keyset walker ((external_id, id) windows of 500); evidence-only rows are cleared without assigning acl
  • Member engine: listing observations and the read watermark land per 500 documents, then only documents whose ACL differs from their observers are rematerialised (rematerializeDocumentAcls, row-bounded pages). Observation changes that must commit with their ACLs (unseen removal, membership rewrite, stale sweep) take at most 25 documents and one page of projection rows; the invariant they keep is per document. The content completion's connector-wide count runs before the lease transaction
  • disableMemberSync: suspends members (which revokes reads at once: a reader needs an active member's observation as well as ACL overlap), revokes ACLs in guarded row-bounded pages, and flips the connector to disabled only after the last page, in one statement that also proves the lease. It was one unguarded SET acl = '{}' under FOR UPDATE that could time out and retry forever. An interrupted or out-of-budget run resumes on the next run; a disable that fails part-way goes through the normal failure path (log failed, lease released) instead of leaving the connector running
  • Stale-member sweep: row-bounded pages with a 60 s tick budget checked before every page; a page that fails transiently (the shared getTransientDatabaseFailure classifier) defers that connector to the next tick and the others are still swept; the scheduler logs a failed sweep and still dispatches

Round trips per 100k unchanged documents

  • Admin permission write: before this PR 1 transaction, ~4,200 statements under the connector share lock; first revision of this PR 4,200 transactions; now 200 transactions of 3 statements, no change reads
  • Member listing observe + materialise: first revision 4,000 transactions of 5 statements; now 200 observation transactions + 200 lock-free reads, no materialise transactions
  • Post-listing rematerialisation: before 1 transaction over every seen document; now 200 lock-free reads, no transactions
  • Workspace restore on a healthy connector: first revision 200 window reads per sync; now none

Type of Change

  • Bug fix

Testing

  • connector-lease-pages.integration.ts (real migrated PostgreSQL 17, wired into both PostgreSQL CI jobs), 18 tests: every ACL assignment is recorded with its transaction and timeouts; each transaction stays within one page under the bounds; a page and a member page waiting on a locked document row hold no knowledge_connector lock (pg_locks); a document above the row cap still lands alone; a lease lost between pages stops further writes while committed pages stay; the pending restore runs before completion and a healthy workspace sync never walks; listing, change-feed and membership rematerialisation page by rows; an interrupted disable resumes to disabled with every ACL empty; a failing disable closes as an ordinary failure; a stale sweep defers a connector a member run holds and still sweeps the others
  • Unit tests for paging, fence-last ordering, lease loss, the row pager, the refresh-returning skip, rematerialisation, sweep deferral/budget, the route still dispatching after a sweep failure, and the content completion count
  • Mutation-checked on file copies: every change (fence order in each lease path, bounds, lock timeout value, row cap and single-document minimum, refresh skip, lock-free pre-select, restore gate, count placement, sweep isolation/budget/busy-connector skip, disable failure path, route isolation, plus the earlier paging mutations) turns at least one test red
  • lib/knowledge + app/api/knowledge unit suites, the knowledge *.postgres.test.ts files, 0021 migration test, related knowledge integration suites on a freshly migrated database, lint, check:audits, type-check

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)

@vercel

vercel Bot commented Sep 23, 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 23, 2026 8:36am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no actionable new issues remain, and all previous findings are resolved.

Summary

This PR bounds connector-lease ACL rewrites to short, projection-row-sized transactions while preserving lease fencing and resumability.

  • Pages ACL updates according to document chunk counts and applies lock and statement timeouts only to ACL-page transactions.
  • Proves connector leases after page writes so transactions do not hold connector locks while waiting for document rows.
  • Makes workspace restoration, member rematerialization, connector disabling, and stale-observation cleanup resumable across bounded pages.
  • Keeps scheduler dispatch running when stale-observation cleanup fails transiently.
  • Adds extensive unit and PostgreSQL integration coverage for page limits, lock ordering, lease loss, deadlines, and interrupted operations.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Select keyset window] --> B[Plan pages from chunk counts]
  B --> C[Heartbeat and check deadline]
  C --> D[Begin bounded transaction]
  D --> E[Lock document rows]
  E --> F[Re-read chunk counts]
  F --> G[Write one ACL page]
  G --> H[Prove connector lease last]
  H --> I[Commit page]
  I --> J{More documents and budget remains?}
  J -->|Yes| A
  J -->|No| K[Finish or retain resumable pending state]
Loading

Reviews (6) · Last reviewed commit: "test(knowledge): expire the restore budg..."

Comment thread apps/sim/lib/knowledge/connectors/member-sync-engine.ts Outdated
Comment thread apps/sim/lib/knowledge/connectors/member-sync-engine.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

Comment thread apps/sim/lib/knowledge/connectors/member-observations.ts

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 21 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/knowledge/connectors/member-sync-engine.ts
Comment thread apps/sim/lib/knowledge/connectors/member-observations.ts
Comment thread apps/sim/lib/knowledge/connectors/sync-persistence.ts
…e-transactions-bounded

# Conflicts:
#	apps/sim/lib/knowledge/connectors/member-sync-engine.ts
… locked chunk counts, and check the sweep budget per page
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 21 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/knowledge/connectors/sync-engine.ts Outdated
Comment thread apps/sim/lib/knowledge/connectors/member-observations.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

Comment thread apps/sim/lib/knowledge/connectors/member-observations.ts Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 21 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 21 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

You've manually re-run cubic several times on this PR. Each manual re-review checks the full PR again and counts toward your usage quota. To preserve your usage limits, we recommend letting cubic automatically review new commits.
Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 21 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

You've manually re-run cubic several times on this PR. Each manual re-review checks the full PR again and counts toward your usage quota. To preserve your usage limits, we recommend letting cubic automatically review new commits.
Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 624793f into staging Sep 23, 2026
34 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/connector-lease-transactions-bounded branch September 23, 2026 15:28

This branch was previously deployed

1 inactive deployment
Preview ac7c2bce Deployed Sep 23, 2026 by vercel[bot]
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