From 7dd05a519cce3a627593d77276e7dbaf514bb2f5 Mon Sep 17 00:00:00 2001 From: Gergo Magyar Date: Thu, 10 Sep 2026 17:19:46 +0000 Subject: [PATCH] Leave an unverified-count checkpoint when embeddings sync cannot count rows. A finished run must not keep an interrupted window marker; the next sync should re-derive the count instead of hitting the identity gate. Co-authored-by: Cursor --- gitnexus/src/cli/embeddings-sync.ts | 20 +++++++++++++++++-- .../test/unit/embeddings-sync-command.test.ts | 9 ++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/gitnexus/src/cli/embeddings-sync.ts b/gitnexus/src/cli/embeddings-sync.ts index b596fde43..a54831c76 100644 --- a/gitnexus/src/cli/embeddings-sync.ts +++ b/gitnexus/src/cli/embeddings-sync.ts @@ -18,6 +18,7 @@ import { decideEmbeddingResume, mintInterruptedCheckpoint, mintPartialCheckpoint, + mintUnverifiedCountCheckpoint, type EmbeddingCheckpoint, type EmbeddingCheckpointProgress, } from '../core/embedding-checkpoint.js'; @@ -26,7 +27,7 @@ import { persistedEmbeddingCountOrUndefined, } from '../core/embedding-count.js'; -/** Add missing embeddings directly to a healthy index, checkpointing every batch. */ +/** Add missing embeddings directly to a healthy index, checkpointing periodically. */ export const embeddingsSyncCommand = async (inputPath?: string): Promise => { const repoPath = inputPath ? path.resolve(inputPath) : getGitRoot(process.cwd()); if (!repoPath) throw new Error('Not inside a git repository. Pass a repository path.'); @@ -135,8 +136,23 @@ export const embeddingsSyncCommand = async (inputPath?: string): Promise = ); const embeddings = await countEmbeddings(); - if (embeddings === undefined) throw new Error('Could not verify persisted embedding count.'); const latest = (await loadMeta(metaDir)) ?? meta; + if (embeddings === undefined) { + // Keep last-known stats.embeddings. An interrupted window marker would + // fail the identity gate on the next run even though this run finished; + // unverified-count is the recovery kind that forces a recount (#2790). + await saveMeta(metaDir, { + ...latest, + embeddingCheckpoint: result.failedNodeIds.length + ? mintPartialCheckpoint(identity, result, resumedFrom) + : mintUnverifiedCountCheckpoint(identity, { + nodesProcessed: result.nodesProcessed, + totalNodes: result.nodesProcessed, + chunksProcessed: result.chunksProcessed, + }), + }); + throw new Error('Could not verify persisted embedding count.'); + } await saveMeta(metaDir, { ...latest, stats: { ...latest.stats, embeddings }, diff --git a/gitnexus/test/unit/embeddings-sync-command.test.ts b/gitnexus/test/unit/embeddings-sync-command.test.ts index f9a60c69c..71e75f558 100644 --- a/gitnexus/test/unit/embeddings-sync-command.test.ts +++ b/gitnexus/test/unit/embeddings-sync-command.test.ts @@ -235,7 +235,14 @@ describe('embeddingsSyncCommand writer safety (#3065)', () => { executeQueryMock.mockResolvedValue([{}]); await expect(run()).rejects.toThrow('Could not verify persisted embedding count.'); - expect(saveMetaMock).not.toHaveBeenCalled(); + expect(saveMetaMock).toHaveBeenCalledTimes(1); + const saved = saveMetaMock.mock.calls[0]?.[1] as { + stats?: { embeddings?: number }; + embeddingCheckpoint?: { kind?: string; pendingNodeIds?: string[] }; + }; + expect(saved.stats?.embeddings).toBe(1); + expect(saved.embeddingCheckpoint?.kind).toBe('unverified-count'); + expect(saved.embeddingCheckpoint?.pendingNodeIds).toEqual([]); expect(releaseMock).toHaveBeenCalled(); });