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 <cursoragent@cursor.com>
This commit is contained in:
Gergo Magyar 2026-09-10 17:19:46 +00:00
parent a5e13a818f
commit 7dd05a519c
2 changed files with 26 additions and 3 deletions

View file

@ -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<void> => {
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<void> =
);
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 },

View file

@ -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();
});