Skip to content

fix(knowledge): settle detach reservations when a knowledge base is purged - #8184

Merged
waleedlatif1 merged 6 commits into
stagingfrom
fix/release-8180-review-findings
Sep 23, 2026
Merged

waleedlatif1 merged 6 commits into
stagingfrom
fix/release-8180-review-findings

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Purging a soft-deleted knowledge base cascaded away a detached connector together with its detach_reserved_bytes, the kept bytes charged to the workspace at removal. The pending detach job then found no knowledge base and settled nothing, so the charge stayed on the workspace and payer ledgers
  • Retention cleanup now calls settleDetachedConnectorReservations after the documents are hard-deleted and before the knowledge base rows are deleted. Per knowledge base, in one transaction, it locks the base then its detached connectors with a non-zero reservation (same order as the detach job), applies the detach job's own settlement (refund a positive remainder, admitted charge for an overdraft; now one shared helper), and zeroes the reservation so a retried purge or a detach run that still reaches the connector settles nothing twice
  • Test drain helper now picks the oldest pending outbox row instead of any row
  • Unfilled-projection fixture writes its Tin row with the base token and lexeme stream the projection trigger uses, so it scopes correctly under real Tin as well as the shim
  • scripts/test-knowledge-acls.ts also runs the 0021 projection source/ACL postgres test, since its setup already applies 0021
  • Purge settles overdrawn detach reservations before deleting documents (usage floors at zero) and the remaining reservations after them, so a partial deletion never leaves the ledger out of step
  • A connector detach pauses (without spending retries) while its knowledge base is deleted, so no release can interleave with the purge; a restore resumes it
  • Fix a flaky memory checkpoint test: tampering overwrote the auth tag with a fixed value, a no-op when the tag already ended with it; it now always changes the last hex digit

Type of Change

  • Bug fix

Testing

  • New purged-detach-reservation.integration.ts (3 tests, real Postgres), wired into the CI knowledge integration step: detach reserves, one bounded run releases part of the source, purge runs; ledger ends at 0 and the late detach job leaves it there. Also covers the zeroing (settle twice, then drain) and the overdraft branch
  • Mutation checks: dropping the cleanup call leaves 198 bytes on the ledger (1 red); dropping the zeroing turns 2 red; dropping the overdraft branch turns 1 red
  • cleanup-soft-deletes.test.ts 27/27 (new ordering test goes red without the call); lib/knowledge/connectors unit tests pass; storage-accounting, search-source-progress, unfilled-projection-source integration pass (32/32 with the new file); 0016 + 0021 postgres tests 23/23
  • bun run lint, bun run check:audits, apps/sim type-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)

…urged

- Retention purge settles a detached connector's remaining detach_reserved_bytes
  (same lock order and settlement branches as the detach job) and zeroes it
  before the knowledge base delete cascades the connector away
- Test drain helper picks the oldest pending outbox row
- Unfilled-projection fixture writes its Tin row the way the projection trigger does
- Knowledge ACL harness also runs the 0021 projection source/ACL postgres test
@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 1:30am UTC

Request Review

@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 8 files

Confidence score: 5/5

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

Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the previously reported accounting and concurrency issues are resolved, and no new actionable failures were found.

Summary

This PR settles detached-connector storage reservations as part of knowledge-base retention cleanup and prevents detach workers from releasing documents while their knowledge base is soft-deleted.

  • Settles overdrawn reservations before document deletion and remaining reservations afterward.
  • Pauses detach outbox work while the parent knowledge base is deleted, without consuming an attempt.
  • Nets per-base reservation settlement into one ledger operation and zeroes settled reservations.
  • Adds PostgreSQL integration coverage for purge, retry, overdraft, restore, and late-detach behavior.
  • Makes related integration fixtures and test helpers deterministic and extends migration-test coverage.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  D[Detached connector reservation] --> O[Settle overdraft]
  O --> H[Hard-delete knowledge documents]
  H --> R[Settle remaining reservation]
  R --> K[Delete knowledge base and cascade connector]
  W[Detach outbox worker] --> C{Knowledge base deleted?}
  C -->|Yes| P[Defer without consuming attempt]
  C -->|Restored| X[Resume bounded document release]
  C -->|Purged| Z[Finish as obsolete]
Loading

Reviews (5) · Last reviewed commit: "fix(knowledge): pause a connector detach..."

Comment thread apps/sim/lib/knowledge/connectors/detachment.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.

@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 9 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/knowledge/connectors/detachment.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.

@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 10 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/background/cleanup-soft-deletes.ts Outdated
Comment thread apps/sim/background/cleanup-soft-deletes.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.

@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 10 files

Confidence score: 5/5

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

Re-trigger cubic

Comment thread apps/sim/background/cleanup-soft-deletes.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.

@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 10 files

Confidence score: 5/5

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

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 420c0df into staging Sep 23, 2026
34 of 35 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/release-8180-review-findings branch September 23, 2026 01:35

This branch was previously deployed

1 inactive deployment
Preview fee77adf 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