mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-12-06 00:37:26 +00:00
* 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>
157 lines
6 KiB
TypeScript
157 lines
6 KiB
TypeScript
/**
|
|
* Jupyter notebooks are indexed as Python by extracting code cells.
|
|
*/
|
|
import { describe, it, expect } from 'vitest';
|
|
import path from 'path';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import {
|
|
getNodesByLabel,
|
|
getNodesByLabelFull,
|
|
getRelationships,
|
|
runPipelineFromRepo,
|
|
writeFixtureRepo,
|
|
} from './helpers.js';
|
|
import { extractNotebookPython } from '../../../src/core/ingestion/ipynb-extractor.js';
|
|
|
|
function pythonNotebook(cells: Array<{ source: string | string[]; language?: string }>): string {
|
|
return JSON.stringify(
|
|
{
|
|
nbformat: 4,
|
|
nbformat_minor: 5,
|
|
metadata: {
|
|
kernelspec: { display_name: 'Python 3', language: 'python', name: 'python3' },
|
|
language_info: { name: 'python' },
|
|
},
|
|
cells: cells.map((c) => ({
|
|
cell_type: 'code',
|
|
metadata: c.language ? { language: c.language } : {},
|
|
source: c.source,
|
|
outputs: [],
|
|
})),
|
|
},
|
|
null,
|
|
2,
|
|
);
|
|
}
|
|
|
|
describe('Jupyter notebook Python pipeline', () => {
|
|
it.each([
|
|
['array', 'ipynb'],
|
|
['string', 'ipynb'],
|
|
['array', 'IPYNB'],
|
|
['string', 'IpYnB'],
|
|
] as const)(
|
|
'maps synthesized fields to notebook source rows (%s, %s)',
|
|
async (format, extension) => {
|
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'gn-ipynb-field-'));
|
|
try {
|
|
const lines = ['class Box:\n', ' def __init__(self):\n', ' self.value = 1\n'];
|
|
const notebook = pythonNotebook([
|
|
{ source: ['# preceding cell\n'] },
|
|
{ source: format === 'array' ? lines : lines.join('') },
|
|
{ source: ['def read(box: Box):\n', ' return box.value\n'] },
|
|
]);
|
|
const filePath = `fields.${extension}`;
|
|
writeFixtureRepo(root, { [filePath]: notebook });
|
|
const result = await runPipelineFromRepo(root, () => {}, { skipGraphPhases: true });
|
|
expect(result.relationshipIntegrity).toMatchObject({ checkedSites: 0 });
|
|
expect(result.relationshipIntegrity?.unverifiedEdges).toBeGreaterThan(0);
|
|
const sourceRow = notebook.split('\n').findIndex((line) => line.includes('self.value = 1'));
|
|
expect(result.graph.getNode(`Property:${filePath}:Box.value`)).toMatchObject({
|
|
properties: { startLine: sourceRow, endLine: sourceRow },
|
|
});
|
|
expect(
|
|
getRelationships(result, 'ACCESSES').filter((edge) => edge.source === 'read'),
|
|
).toEqual([expect.objectContaining({ target: 'value', targetFilePath: filePath })]);
|
|
} finally {
|
|
fs.rmSync(root, { recursive: true, force: true });
|
|
}
|
|
},
|
|
60000,
|
|
);
|
|
|
|
it('keeps class methods out of notebook module bindings', async () => {
|
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'gn-ipynb-scope-'));
|
|
try {
|
|
const declarations = 'class Box:\n def target(self):\n return 1\n';
|
|
writeFixtureRepo(root, {
|
|
'lib.py': 'def target():\n return 2\n',
|
|
'unbound.ipynb': pythonNotebook([
|
|
{ source: declarations },
|
|
{ source: 'def unbound_caller():\n return target()\n' },
|
|
]),
|
|
'imported.ipynb': pythonNotebook([
|
|
{ source: 'from lib import target\n' },
|
|
{ source: declarations },
|
|
{ source: 'def imported_caller():\n return target()\n' },
|
|
]),
|
|
});
|
|
const result = await runPipelineFromRepo(root, () => {}, { skipGraphPhases: true });
|
|
const calls = getRelationships(result, 'CALLS').filter((edge) => edge.target === 'target');
|
|
expect(calls.filter((edge) => edge.rel.targetId.includes('Box.target'))).toEqual([]);
|
|
expect(
|
|
calls
|
|
.filter((edge) => edge.source === 'imported_caller')
|
|
.map((edge) => edge.targetFilePath),
|
|
).toEqual(['lib.py']);
|
|
} finally {
|
|
fs.rmSync(root, { recursive: true, force: true });
|
|
}
|
|
}, 60000);
|
|
|
|
it('indexes notebook functions, imports sibling py, and skip-fails closed', async () => {
|
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'gn-ipynb-'));
|
|
try {
|
|
writeFixtureRepo(root, {
|
|
'lib.py': 'def helper():\n return 1\n',
|
|
'analysis.ipynb': pythonNotebook([
|
|
{ source: ['from lib import helper\n'] },
|
|
{ source: ['def train():\n', ' return helper()\n'] },
|
|
]),
|
|
'broken.ipynb': '{not json',
|
|
'julia.ipynb': JSON.stringify({
|
|
nbformat: 4,
|
|
nbformat_minor: 5,
|
|
metadata: {
|
|
kernelspec: { language: 'julia', name: 'julia', display_name: 'Julia' },
|
|
},
|
|
cells: [
|
|
{ cell_type: 'code', metadata: {}, source: ['function train() end\n'], outputs: [] },
|
|
],
|
|
}),
|
|
});
|
|
const result = await runPipelineFromRepo(root, () => {}, { skipGraphPhases: true });
|
|
const functions = getNodesByLabel(result, 'Function');
|
|
expect(functions).toContain('train');
|
|
expect(functions).toContain('helper');
|
|
const train = getNodesByLabelFull(result, 'Function').find((n) => n.name === 'train');
|
|
expect(train?.properties.filePath.replace(/\\/g, '/')).toMatch(/analysis\.ipynb$/);
|
|
const extracted = extractNotebookPython(
|
|
fs.readFileSync(path.join(root, 'analysis.ipynb'), 'utf8'),
|
|
)!;
|
|
expect(train?.properties.startLine).toBe(extracted.segments[1].jsonStartLine);
|
|
const imports = getRelationships(result, 'IMPORTS');
|
|
expect(
|
|
imports.some(
|
|
(e) =>
|
|
e.sourceFilePath.replace(/\\/g, '/').endsWith('analysis.ipynb') &&
|
|
(e.target === 'helper' || e.targetFilePath.replace(/\\/g, '/').endsWith('lib.py')),
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
getNodesByLabelFull(result, 'Function').filter(
|
|
(n) =>
|
|
n.properties.filePath.replace(/\\/g, '/').endsWith('julia.ipynb') && n.name === 'train',
|
|
),
|
|
).toHaveLength(0);
|
|
expect(
|
|
getNodesByLabelFull(result, 'Function').filter((n) =>
|
|
n.properties.filePath.replace(/\\/g, '/').endsWith('broken.ipynb'),
|
|
),
|
|
).toHaveLength(0);
|
|
} finally {
|
|
fs.rmSync(root, { recursive: true, force: true });
|
|
}
|
|
}, 60000);
|
|
});
|