GitNexus/gitnexus/test/unit/cache-interning-benchmark.test.ts
Gergő Magyar 612136cadc
Some checks are pending
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / Classify release event (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / ci (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
fix: compact incremental graphs adaptively (#3854)
* fix: rebuild changed graphs in fresh databases

Populated-table writes inflate LadybugDB storage (#3852). Bulk-load
fresh graphs while retaining parse caches, no-ops and logical counts.

Read prior graphs without writes so staging failures preserve their
bytes. Restore compatible vectors and rebuild derived layers and FTS
under the existing checkpoint and publication guards.

Keep the write policy, preservation guards and regressions together
so the replacement has one coherent publication contract.

Closes #3852

* perf: skip FTS builds for empty tables

* fix: align CI with compact graph writes

Validate populated-table FTS coverage and cached full writes while keeping the performance budget unchanged. Compare database bytes without enumerating Buffer keys, and retain native checkpoint failure details.

* perf: use bounded hybrid graph writes

Keep small edits selective and scale the mutation budget with graph size.
Compact accumulated native storage growth without copying every graph on
every edit. Preserve physical receipts across shared-store publication and
native resource limits during recovery.

Align CI assertions with selective writes and populated FTS tables.
Refs #3852

* fix: close hybrid compaction gaps

Count removed rows before selective writes and preserve the shared
graph's compaction receipt when publishing completed vectors.

* test: serialize compaction native suites

* test: declare capped count determinism

* test: mock persisted rows in FTS fixtures

* fix: address hybrid write review (#3854)

Retain unreadable graph bytes before recovery and stop destructive writes
when the real embedding cache probe fails.

Keep small deletions and renames selective, preserve healthy private
graphs through edit/revert cycles, and bound FTS presence probes.

Cover durability, footprint boundaries and vector restoration. Align
mock graph fixtures and fix the pre-existing WAL test's UUID false
positive found by full-suite validation; its nine-test suite now passes.

* test: clarify selective restore fixture (#3854)

* test: isolate recovery CI fixtures (#3854)

Cap embedding generation in the missing-graph recovery fixture so it does
not require optional model packages. Allow cold PowerShell startup in the
OS identity smoke test; retain the five-second production probe limit.

* test: finish review cleanup (#3854)

* test: strengthen graph integrity checks

Hash persisted node and edge properties; clean failed fixtures and gate FTS setup through the test lifecycle.

Refs #3854

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-09 20:57:07 +00:00

196 lines
8 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { mkdtempSync, readFileSync, rmSync, writeFileSync, mkdirSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
const checker = new URL('../../bench/v8-sidecar/check.mjs', import.meta.url);
const { assessPairs, indexingMetrics, interningMetrics } = await import(checker.href);
const indexing = (lifecycle = 'fresh') => ({
lifecycle,
screen: false,
pairs: 9,
head: 'same-source',
node: process.version,
results: Array.from({ length: 10 }, (_, pair) =>
['baseline', 'candidate'].map((variant) => ({
name: variant + '-' + (pair || 'seed'),
variant,
ms: variant === 'baseline' ? 100 : 90,
cpuMs: variant === 'baseline' ? 100 : 90,
incremental: { writeMode: 'incremental', changedFiles: 1 },
rebuildReasons: [] as string[],
cachedFiles: 100,
graph: { nodes: { count: 3, sha256: 'nodes' }, edges: { count: 2, sha256: 'edges' } },
persistedTargets: [{ target: pair % 2 ? 'load' : 'save' }],
})),
).flat(),
});
const interning = () => ({
equivalent: true,
shards: 2,
bytes: 1000,
strings: 10,
warmupRounds: 2,
corpusHead: 'same-source',
node: process.version,
samples: Array.from({ length: 10 }, (_, round) =>
['baseline', 'candidate'].map((variant) => ({
round,
variant,
ms: 100,
cpuMs: 100,
hydrateCpuMs: 200,
})),
).flat(),
});
describe('cache interning performance gate', () => {
it.each([0.7, 1, 1.05, 1.051, 1.2])('enforces the paired 5%% budget (%s)', (ratio) => {
const pairs = Array.from({ length: 8 }, () => ({ baseline: 100, candidate: 100 * ratio }));
expect(assessPairs(pairs).verdict).toBe(ratio > 1.05 ? 'regression' : 'within-budget');
});
it.each([0, -1, NaN, Infinity, undefined])('rejects invalid timing (%s)', (value) => {
const pairs = Array.from({ length: 8 }, () => ({ baseline: 100, candidate: value }));
expect(() => assessPairs(pairs)).toThrow('timing');
});
it('rejects a slower overall median even when most paired ratios improve', () => {
const pairs = [100, 100, 100, 100, 200, 200, 200, 200].map((baseline, i) => ({
baseline,
candidate: baseline * (i < 3 ? 2 : 0.9),
}));
const result = assessPairs(pairs);
expect(result.pairedMedianRatio).toBeLessThan(1);
expect(result.medianRatio).toBeGreaterThan(1.05);
expect(result.verdict).toBe('regression');
});
it('excludes warm-up but keeps every measured pair', () => {
const report = indexing();
report.results[1].ms = 10000;
report.results[3].ms = 10000;
expect(indexingMetrics(report, 'fresh')[0].candidateMedian).toBe(90);
report.results
.filter((r) => r.variant === 'candidate')
.forEach((r) => {
r.cpuMs = 120;
});
expect(indexingMetrics(report, 'fresh')[1].verdict).toBe('regression');
});
it('accepts compact full writes for cached edits without relaxing the performance budget', () => {
const report = indexing();
for (const sample of report.results.filter((row) => row.variant === 'candidate')) {
sample.incremental.writeMode = 'full';
sample.rebuildReasons = ['escalated-full-write'];
sample.cpuMs = 120;
}
expect(indexingMetrics(report, 'fresh')[1].verdict).toBe('regression');
report.results[3].cachedFiles = 0;
expect(() => indexingMetrics(report, 'fresh')).toThrow('hydrate cached files');
});
it('rejects cache invalidations even alongside a compact full-write reason', () => {
const report = indexing();
report.results[3].incremental.writeMode = 'full';
report.results[3].rebuildReasons = ['escalated-full-write', 'runner-identity'];
expect(() => indexingMetrics(report, 'fresh')).toThrow('invalidate the parse cache');
});
it.each(['', 'unknown', undefined])('rejects an unrecognized write mode (%s)', (mode) => {
const report = indexing();
report.results[3].incremental = { ...report.results[3].incremental, writeMode: mode as string };
expect(() => indexingMetrics(report, 'fresh')).toThrow();
});
it.each(['missing', 'duplicate', 'screened', 'cold', 'wrong-target', 'graph', 'rebuild'])(
'rejects invalid indexing evidence (%s)',
(kind) => {
const report = indexing();
if (kind === 'missing') report.results.pop();
if (kind === 'duplicate') report.results[3] = report.results[2];
if (kind === 'screened') report.screen = true;
if (kind === 'cold') report.results[3].cachedFiles = 0;
if (kind === 'wrong-target') report.results[3].persistedTargets = [{ target: 'save' }];
if (kind === 'graph') report.results[3].graph.edges.sha256 = 'wrong';
if (kind === 'rebuild') report.results[3].rebuildReasons = ['cache-miss'];
expect(() => indexingMetrics(report, 'fresh')).toThrow();
},
);
it('requires pair-bound validated projection receipts when graph IDs migrate', () => {
const report = indexing();
const candidate = report.results.find((sample) => sample.name === 'candidate-1')!;
candidate.graph.edges.sha256 = 'full-span-edges';
expect(() => indexingMetrics(report, 'fresh')).toThrow(/without a parity receipt/);
const graphParity = Array.from({ length: 10 }, (_, i) => {
const suffix = i || 'seed';
const baseline = report.results.find((sample) => sample.name === `baseline-${suffix}`)!;
const after = report.results.find((sample) => sample.name === `candidate-${suffix}`)!;
return {
version: 1,
mode: i === 1 ? 'point-to-span' : 'exact',
baselineName: baseline.name,
candidateName: after.name,
baseline: structuredClone(baseline.graph),
candidate: structuredClone(after.graph),
comparison: structuredClone(baseline.graph),
};
});
expect(() => indexingMetrics({ ...report, graphParity }, 'fresh')).not.toThrow();
expect(() =>
indexingMetrics({ ...report, graphParity: graphParity.slice(1) }, 'fresh'),
).toThrow(/Incomplete graph parity/);
const modified = structuredClone(graphParity);
modified[1].comparison.edges.sha256 = 'invented';
expect(() => indexingMetrics({ ...report, graphParity: modified }, 'fresh')).toThrow(
/projection differs/,
);
const misbound = structuredClone(graphParity);
misbound[1].candidate.edges.sha256 = 'other';
expect(() => indexingMetrics({ ...report, graphParity: misbound }, 'fresh')).toThrow(
/candidate does not match/,
);
});
it('rejects missing, duplicate, or non-equivalent shard measurements', () => {
const report = interning();
expect(interningMetrics(report)).toHaveLength(3);
expect(() => interningMetrics({ ...report, equivalent: false })).toThrow('equivalence');
expect(() => interningMetrics({ ...report, samples: report.samples.slice(1) })).toThrow(
'Incomplete',
);
report.samples[1] = report.samples[0];
expect(() => interningMetrics(report)).toThrow('Duplicate');
});
it('fails the CLI on CPU regressions and retains the diagnostic report', () => {
const directory = mkdtempSync(path.join(tmpdir(), 'intern-gate-'));
try {
for (const lifecycle of ['fresh', 'persistent']) {
mkdirSync(path.join(directory, lifecycle));
const report = indexing(lifecycle);
if (lifecycle === 'fresh')
report.results
.filter((r) => r.variant === 'candidate')
.forEach((r) => {
r.cpuMs = 120;
});
writeFileSync(path.join(directory, lifecycle, 'results.json'), JSON.stringify(report));
}
writeFileSync(path.join(directory, 'interner.json'), JSON.stringify(interning()));
const result = spawnSync(process.execPath, [fileURLToPath(checker), directory], {
encoding: 'utf8',
});
expect(result.status, result.stderr).toBe(1);
expect(readFileSync(path.join(directory, 'report.md'), 'utf8')).toContain('regression');
} finally {
rmSync(directory, { recursive: true, force: true });
}
});
});