Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(knowledge): purge trashed Google Sheets tabs on a normal sync#5883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -244,6 +244,47 @@ export function shouldReconcileDeletions( | ||
| return !syncContext?.listingCapped || Boolean(fullSync) | ||
| } | ||
| /** | ||
| * Decides whether a zero-document listing should skip deletion reconciliation. | ||
| * | ||
| * An empty listing is normally indistinguishable from a provider outage, so | ||
| * reconciliation is skipped by default rather than risk wiping a knowledge base on | ||
| * a bad response. A connector can set `sourceConfirmedEmpty` on `syncContext` to | ||
| * vouch that it verified the empty result directly against the source — not | ||
| * merely an empty listing page — e.g. a single-resource connector confirming its | ||
| * one source item was trashed/removed via a dedicated metadata lookup. | ||
| */ | ||
| export function shouldSkipEmptyListing( | ||
| externalDocsCount: number, | ||
| existingDocsCount: number, | ||
| fullSync: boolean | undefined, | ||
| syncContext: Record<string, unknown> | undefined | ||
| ): boolean { | ||
| if (externalDocsCount !== 0 || existingDocsCount === 0 || fullSync) return false | ||
| return !syncContext?.sourceConfirmedEmpty | ||
| } | ||
| /** | ||
| * Decides whether a deletion should be blocked by the mass-deletion safety | ||
| * threshold (more than half of existing documents, over 5) instead of proceeding. | ||
| * | ||
| * This guards against a connector-side bug or transient glitch producing a | ||
| * listing that looks mostly empty. `sourceConfirmedEmpty` bypasses it the same | ||
| * way it bypasses `shouldSkipEmptyListing` — the connector positively verified | ||
| * the deletion against the source rather than merely inferring it from a | ||
| * listing, so the extra caution this threshold provides doesn't apply. | ||
| */ | ||
| export function exceedsDeletionSafetyThreshold( | ||
| removedCount: number, | ||
| existingCount: number, | ||
| fullSync: boolean | undefined, | ||
| syncContext: Record<string, unknown> | undefined | ||
| ): boolean { | ||
| if (fullSync || syncContext?.sourceConfirmedEmpty) return false | ||
| const deletionRatio = existingCount > 0 ? removedCount / existingCount : 0 | ||
| return deletionRatio > 0.5 && removedCount > 5 | ||
| } | ||
| /** | ||
| * Resolves tag values from connector metadata using the connector's mapTags function. | ||
| * Translates semantic keys returned by mapTags to actual DB slots using the | ||
| @@ -537,7 +578,14 @@ export async function executeSync( | ||
| const excludedExternalIds = new Set(excludedDocs.map((d) => d.externalId).filter(Boolean)) | ||
| if (externalDocs.length === 0 && existingDocs.length > 0 && !options?.fullSync) { | ||
| if ( | ||
| shouldSkipEmptyListing( | ||
| externalDocs.length, | ||
| existingDocs.length, | ||
| options?.fullSync, | ||
| syncContext | ||
| ) | ||
| ) { | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| logger.warn( | ||
| `Source returned 0 documents but ${existingDocs.length} exist — skipping reconciliation`, | ||
| { connectorId } | ||
| @@ -799,12 +847,20 @@ export async function executeSync( | ||
| .map((d) => d.id) | ||
| if (removedIds.length > 0) { | ||
| const deletionRatio = existingDocs.length > 0 ? removedIds.length / existingDocs.length : 0 | ||
| if (deletionRatio > 0.5 && removedIds.length > 5 && !options?.fullSync) { | ||
| if ( | ||
| exceedsDeletionSafetyThreshold( | ||
| removedIds.length, | ||
| existingDocs.length, | ||
| options?.fullSync, | ||
| syncContext | ||
| ) | ||
| ) { | ||
| logger.warn( | ||
| `Skipping deletion of ${removedIds.length}/${existingDocs.length} docs — exceeds safety threshold. Trigger a full sync to force cleanup.`, | ||
| { connectorId, deletionRatio: Math.round(deletionRatio * 100) } | ||
| { | ||
| connectorId, | ||
| deletionRatio: Math.round((removedIds.length / existingDocs.length) * 100), | ||
| } | ||
| ) | ||
| } else { | ||
| await hardDeleteDocuments(removedIds, syncLogId) | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.