(args: {
directory: string;
projectIdentity: string;
db: Database;
gitCommitIndexing: { enabled: boolean; since_days: number; max_commits: number };
})
| 439 | } |
| 440 | |
| 441 | async function sweepGitCommits(args: { |
| 442 | directory: string; |
| 443 | projectIdentity: string; |
| 444 | db: Database; |
| 445 | gitCommitIndexing: { enabled: boolean; since_days: number; max_commits: number }; |
| 446 | }): Promise<void> { |
| 447 | const { directory, projectIdentity, db, gitCommitIndexing } = args; |
| 448 | const holderId = crypto.randomUUID(); |
| 449 | const lease = acquireGitSweepLease(db, projectIdentity, holderId); |
| 450 | if (!lease.acquired) { |
| 451 | const reason = |
| 452 | lease.reason === "cooldown_active" |
| 453 | ? `cooldown active until ${lease.nextAllowedAt}` |
| 454 | : `lease held by ${lease.leaseHolder ?? "another holder"} until ${lease.leaseExpiresAt ?? "unknown"}`; |
| 455 | log(`[git-commits] sweep skipped for ${projectIdentity}: ${reason}`); |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | const startedAt = Date.now(); |
| 460 | const stopRenewal = startGitSweepLeaseRenewal(db, projectIdentity, holderId); |
| 461 | log( |
| 462 | `[git-commits] sweep starting for ${projectIdentity} (sinceDays=${gitCommitIndexing.since_days} maxCommits=${gitCommitIndexing.max_commits})`, |
| 463 | ); |
| 464 | try { |
| 465 | const result = await indexCommitsForProject(db, projectIdentity, directory, { |
| 466 | sinceDays: gitCommitIndexing.since_days, |
| 467 | maxCommits: gitCommitIndexing.max_commits, |
| 468 | }); |
| 469 | if (result.nonIndexable) { |
| 470 | // Not a repo / repo with no commits: park on the long re-probe |
| 471 | // cooldown so the timer doesn't retry (and log) every tick. |
| 472 | if (!parkGitSweepNonIndexable(db, projectIdentity, holderId)) { |
| 473 | releaseGitSweepLease(db, projectIdentity, holderId); |
| 474 | } |
| 475 | return; |
| 476 | } |
| 477 | // Drain any remaining embedding backlog from this sweep (indexer caps per run). |
| 478 | let drainedEmbeddings = 0; |
| 479 | if (result.embedded > 0) { |
| 480 | drainedEmbeddings = await embedUnembeddedCommits(db, projectIdentity); |
| 481 | } |
| 482 | const cooldownMarked = markGitSweepSuccessAndRelease(db, projectIdentity, holderId); |
| 483 | if (!cooldownMarked) { |
| 484 | releaseGitSweepLease(db, projectIdentity, holderId); |
| 485 | log( |
| 486 | `[git-commits] sweep finished for ${projectIdentity}, but lease was no longer active; cooldown not advanced`, |
| 487 | ); |
| 488 | } |
| 489 | |
| 490 | const memorySnapshot = getProjectEmbeddingSnapshot(projectIdentity); |
| 491 | let backlogDrained = 0; |
| 492 | if (memorySnapshot?.gitCommitEnabled) { |
| 493 | try { |
| 494 | backlogDrained = await drainCommitBacklogForProject( |
| 495 | db, |
| 496 | projectIdentity, |
| 497 | Date.now() + GIT_COMMIT_BACKLOG_DRAIN_MAX_MS, |
| 498 | ); |
no test coverage detected