This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/aitimemachine/gitnexus/test/integration/python-import-index-reuse.test.ts
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

88 lines
3.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Production-path regression guard for PR #1918 review finding P1.
*
* The Python file index (`getPythonFileIndex` in `import-target.ts`) is
* memoized on the `allFilePaths` Set identity via a WeakMap. The registry-
* primary path reaches it through `pythonScopeResolver.resolveImportTarget`
* (the orchestrator adapter) — NOT by calling `resolvePythonImportTarget`
* directly the way the unit parity test does. Before the fix, that adapter
* copied the set (`new Set(allFilePaths)`) on every import, handing a fresh
* WeakMap key per call so the index rebuilt every import (O(imports × files)).
*
* This test drives the adapter exactly as the orchestrator does and asserts the
* index is built ONCE across many imports on a stable set. It fails (build
* count == number of imports) if the per-import copy is reintroduced.
*/
import { describe, it, expect } from 'vitest';
import { pythonScopeResolver } from '../../src/core/ingestion/languages/python/scope-resolver.js';
import {
getPythonFileIndexBuildCount,
resetPythonFileIndexBuildCount,
} from '../../src/core/ingestion/languages/python/index-stats.js';
/**
* A synthetic workspace: a real package (`realpkg/__init__.py`, so the
* `hasRepoCandidate` gate passes) plus many unrelated modules. The imports
* below are multi-segment and miss every fast path, so each call reaches both
* `hasRepoCandidate` and `resolveAbsoluteFromFiles` — the two index consumers.
*/
function buildWorkspace(fileCount: number): Set<string> {
const files = new Set<string>();
for (let i = 0; i < fileCount; i++) {
files.add(`pkg/sub/mod${String(i).padStart(5, '0')}.py`);
}
files.add('realpkg/__init__.py');
files.add('realpkg/widget.py');
return files;
}
describe('Python import resolution — index reuse across imports (PR #1918 P1)', () => {
it('builds the file index once for many imports over a stable file set', () => {
const allFilePaths = buildWorkspace(300);
const fromFile = 'app/main.py';
const importCount = 300;
resetPythonFileIndexBuildCount();
for (let i = 0; i < importCount; i++) {
// Multi-segment, candidate-passing, suffix-miss → reaches the index.
pythonScopeResolver.resolveImportTarget(`realpkg.ghost${i}`, fromFile, allFilePaths);
}
// The whole point of PR #1918: O(imports + files), not O(imports × files).
// Pre-fix this was 300 (one rebuild per import via the adapter's Set copy).
expect(getPythonFileIndexBuildCount()).toBe(1);
});
it('rebuilds once per distinct file set (per-run isolation, no stale reuse)', () => {
const fromFile = 'app/main.py';
resetPythonFileIndexBuildCount();
const setA = buildWorkspace(50);
for (let i = 0; i < 20; i++) {
pythonScopeResolver.resolveImportTarget(`realpkg.ghost${i}`, fromFile, setA);
}
expect(getPythonFileIndexBuildCount()).toBe(1);
// A different Set instance is a different logical workspace → one more build.
const setB = buildWorkspace(50);
for (let i = 0; i < 20; i++) {
pythonScopeResolver.resolveImportTarget(`realpkg.ghost${i}`, fromFile, setB);
}
expect(getPythonFileIndexBuildCount()).toBe(2);
});
it('still resolves real imports correctly (the perf test is not vacuous)', () => {
const allFilePaths = buildWorkspace(20);
const fromFile = 'app/main.py';
// Suffix-fallback hit through the adapter: realpkg.widget → realpkg/widget.py.
expect(pythonScopeResolver.resolveImportTarget('realpkg.widget', fromFile, allFilePaths)).toBe(
'realpkg/widget.py',
);
// Gated-out / unresolvable import returns null.
expect(
pythonScopeResolver.resolveImportTarget('realpkg.ghost', fromFile, allFilePaths),
).toBeNull();
});
});