GitNexus/gitnexus/test/unit/mcp-query-integrity-parity.test.ts
Gergő Magyar 3f2f3ce18e
fix(graph): validate reference sites and dedupe context (#3845)
* fix(context): dedupe references before limits

Select distinct relationship types and symbols in all seven graph
queries before applying their existing 30-row limits. Repeated call
sites must not hide other callers.

Refs #3500

* fix(graph): validate reference provenance

Check source identities and spans before either writer drops provenance.
Retain full spans so nested calls sharing a start stay distinct. Reject
invalid records without publishing a clean receipt; preserve the prior
published index on failure.

Expose repository-wide ingestion receipts to MCP callers. Legacy,
collapsed, point-only and extracted-buffer provenance stays unverified.
Identical identities keep the existing first-writer-wins evidence policy.

Refs #3500

* fix(graph): leave notebook spans unverified

Notebook captures map JSON rows but retain extracted-code columns. Keep these identities explicitly unverified instead of certifying a span that no single source buffer can validate.

Refs #3500

* feat(graph): log ingestion integrity checks

Record summary counts, bounded debug examples, and actionable rejection diagnostics before provenance is discarded. Document the debug command and verify both graph writers retain the logging contract.

Refs #3500

* refactor(graph): simplify reference validation

Refs #3500

* fix(graph): harden reference integrity checks

Reject conflicting sites across streaming partitions.
Preserve DISTINCT limits with indexed label anchors.
Verify span migrations without weakening benchmark gates.

Refs #3500, #3845.

* fix(graph): validate canonical reference spans

Reject access-kind prefixes and use parser UTF-16 column units for source bounds. Cover both writers with malformed identities and native Unicode parser regressions.

Refs #3500, #3845

* test(graph): verify streamed integrity runs

Force streaming in cold and changed-source cases and assert the emitted manifest. Clarify complete-span identity and successful Cypher response docs.

Refs #3500, #3845

* fix(graph): preserve parsed-source provenance

Keep concurrent source edits unverified, bound streamed identity probes,
and distinguish transient MCP uncertainty from missing receipts.

Refs #3845, #3500

* fix(graph): avoid retaining skipped endpoint IDs

Resolve parser identity before allocating test directories.

Refs #3845, #3500

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-08 20:55:14 +00:00

93 lines
3.8 KiB
TypeScript

import { describe, expect, it } from 'vitest';
const { parityBody } = await import(
new URL('../../bench/mcp-query/integrity-parity.mjs', import.meta.url).href
);
const receipt = { version: 1, checkedSites: 4, unverifiedEdges: 0, duplicatesSuppressed: 1 };
const verified = {
status: 'verified',
scope: 'source-reference-ingestion',
checkedSites: 4,
unverifiedEdges: 0,
duplicatesSuppressed: 1,
note: 'Source-reference identities and source spans were checked before writing. This validates ingestion provenance, not resolution accuracy or query completeness.',
};
const indexes = { baseline: {}, candidate: { relationshipIntegrity: receipt } };
describe('MCP benchmark legacy receipt parity', () => {
it('validates and removes only the candidate receipt for a legacy baseline', () => {
const baseline = { definitions: [{ id: 'one' }] };
const candidate = { ...baseline, graph_integrity: verified };
expect(parityBody(baseline, 'baseline', indexes)).toEqual(baseline);
expect(parityBody(candidate, 'candidate', indexes)).toEqual(baseline);
expect(candidate.graph_integrity).toEqual(verified);
expect(
parityBody({ ...candidate, definitions: [{ id: 'different' }] }, 'candidate', indexes),
).not.toEqual(baseline);
});
it('validates the unverified status and its counts against the candidate index', () => {
const candidateReceipt = { ...receipt, unverifiedEdges: 2 };
const graph_integrity = {
...verified,
status: 'unverified',
unverifiedEdges: 2,
note: 'Some reference edges have only a caller/target identity, extracted-buffer coordinates, or incomplete source provenance. Their original source-site distinctness is unverified; query deduplication is not an integrity check.',
};
expect(
parityBody({ graph_integrity }, 'candidate', {
baseline: {},
candidate: { relationshipIntegrity: candidateReceipt },
}),
).toEqual({});
});
it.each([
undefined,
{ ...receipt, version: 2 },
{ ...receipt, checkedSites: -1 },
{ ...receipt, unverifiedEdges: 0.5 },
{ ...receipt, duplicatesSuppressed: Number.MAX_SAFE_INTEGER + 1 },
])('rejects missing or malformed candidate index receipt %#', (invalid) => {
expect(() =>
parityBody({ graph_integrity: verified }, 'candidate', {
baseline: {},
candidate: { relationshipIntegrity: invalid },
}),
).toThrow('Candidate index has no valid relationship integrity receipt');
});
it.each([
undefined,
{ ...verified, status: 'unverified' },
{ ...verified, checkedSites: 5 },
{ ...verified, scope: 'other' },
{ ...verified, note: 'wrong' },
{ ...verified, extra: true },
])('rejects a missing or incorrect response receipt %#', (invalid) => {
expect(() => parityBody({ graph_integrity: invalid }, 'candidate', indexes)).toThrow(
'Candidate graph_integrity does not match its index receipt',
);
});
it('rejects a baseline response carrying an unexpected receipt', () => {
expect(() => parityBody({ graph_integrity: verified }, 'baseline', indexes)).toThrow(
'Legacy baseline unexpectedly returned graph_integrity',
);
});
it('keeps receipts in ordinary parity when both indexes have them', () => {
const both = {
baseline: { relationshipIntegrity: receipt },
candidate: { relationshipIntegrity: receipt },
};
const baseline = { graph_integrity: verified, definitions: [] };
const candidate = { graph_integrity: { ...verified, checkedSites: 5 }, definitions: [] };
expect(parityBody(baseline, 'baseline', both)).toBe(baseline);
expect(parityBody(candidate, 'candidate', both)).toBe(candidate);
expect(parityBody(candidate, 'candidate', both)).not.toEqual(
parityBody(baseline, 'baseline', both),
);
});
});