Skip to content

Commit ed2e401

Browse files
committed
fix(connectors): keep counting every slice until a connector's first pass finishes
A first pass's own unfinished slices wrote a count, so later slices of that pass saw it and stopped counting. Skip only once lastSyncAt shows a finished pass, which unfinished slices never advance.
1 parent aa1ed38 commit ed2e401

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

‎apps/sim/lib/knowledge/connectors/sync-engine.test.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,11 +1592,22 @@ describe('completeSuccessfulSync', () => {
15921592
})
15931593

15941594
it.each([
1595-
{ recordedDocCount: 7, written: undefined },
1596-
{ recordedDocCount: null, written: 4 },
1595+
{
1596+
scenario: 'after a finished pass',
1597+
lastSyncAt: new Date(),
1598+
lastSyncDocCount: 7,
1599+
written: undefined,
1600+
},
1601+
{ scenario: 'with no count yet', lastSyncAt: null, lastSyncDocCount: null, written: 4 },
1602+
{
1603+
scenario: 'on a later slice of the first pass',
1604+
lastSyncAt: null,
1605+
lastSyncDocCount: 3,
1606+
written: 4,
1607+
},
15971608
])(
1598-
'leaves an unfinished pass on the last finished count unless none was recorded: $recordedDocCount',
1599-
async ({ recordedDocCount, written }) => {
1609+
'counts an unfinished slice only until a pass has finished: $scenario',
1610+
async ({ lastSyncAt, lastSyncDocCount, written }) => {
16001611
const { completeSuccessfulSync } = await import('@/lib/knowledge/connectors/sync-engine')
16011612
queueTableRows(schemaMock.knowledgeBase, [{ id: 'kb-1' }])
16021613
queueTableRows(schemaMock.knowledgeConnector, [{ id: 'c-1', lastSyncDocCount: 7 }])
@@ -1624,7 +1635,7 @@ describe('completeSuccessfulSync', () => {
16241635
},
16251636
null,
16261637
false,
1627-
recordedDocCount
1638+
{ lastSyncAt, lastSyncDocCount }
16281639
)
16291640
).resolves.toBe(true)
16301641

‎apps/sim/lib/knowledge/connectors/sync-engine.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ export async function completeSuccessfulSync(
400400
directoryNotice: string | null = null,
401401
/** A pending ACL rewrite this run began but did not finish: the flag stays and the next run resumes it. */
402402
accessRewriteUnfinished = false,
403-
/** The count the connector held when this run took its lock. */
404-
recordedDocCount: number | null = null
403+
/** The connector as this run locked it: whether a pass has finished, and the count it holds. */
404+
lockedConnector: { lastSyncAt: Date | null; lastSyncDocCount: number | null } | null = null
405405
): Promise<boolean> {
406406
const processingDispatchFailed = result.processingDispatch.failed > 0
407407
const contentNotice =
@@ -434,13 +434,17 @@ export async function completeSuccessfulSync(
434434
* adds and deletes would drift; when it cannot be read the previous count stands, which the
435435
* guard already treats as a floor.
436436
*
437-
* A slice that leaves its content pass unfinished keeps the count the last finished pass
438-
* recorded, the same pass `lastSyncAt` still names, so a large connector is not counted again
439-
* on every slice of a pass whose count would only be replaced when it finishes. A connector
440-
* with no count yet is still counted, so a first pass shows its progress.
437+
* Once a pass has finished, a slice that leaves the next pass unfinished keeps the count that
438+
* finished pass recorded, the same pass `lastSyncAt` still names, so a large connector is not
439+
* counted again on every slice of a pass whose count is replaced when it finishes. Unfinished
440+
* slices never advance `lastSyncAt`, so until a pass finishes every slice is counted and a
441+
* first pass shows its progress.
441442
*/
442443
const keepRecordedCount =
443-
contentPass !== undefined && !contentPass.complete && recordedDocCount !== null
444+
contentPass !== undefined &&
445+
!contentPass.complete &&
446+
lockedConnector?.lastSyncAt != null &&
447+
lockedConnector.lastSyncDocCount !== null
444448
const actualDocCount = keepRecordedCount
445449
? null
446450
: await countLiveConnectorDocuments(connectorId).catch((error: unknown) => {
@@ -1429,7 +1433,7 @@ export async function executeSync(
14291433
contentPass,
14301434
directoryNotice,
14311435
accessRewriteUnfinished,
1432-
connector.lastSyncDocCount
1436+
connector
14331437
)
14341438

14351439
if (!completionLanded) {

0 commit comments

Comments
 (0)