1
0

context-ranking.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Context ranking: common-word precision + low-confidence handoff.
  3. *
  4. * Regression coverage for the failure where a prose query
  5. * ("capture intro onboarding screen flat object") surfaced an unrelated
  6. * constant named `FLAT` (in a download script) as a top entry point — because
  7. * the descriptive word "flat" exact-matched it and the +exact-name bonus was
  8. * exempt from single-term dampening. The fix: only distinctive identifiers earn
  9. * that exemption; an isolated common-word exact match is demoted, and a query
  10. * that resolves only to such weak matches is flagged low-confidence so the
  11. * response hands off to explore/trace instead of bluffing.
  12. */
  13. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  14. import * as fs from 'fs';
  15. import * as path from 'path';
  16. import * as os from 'os';
  17. import CodeGraph from '../src/index';
  18. import { LOW_CONFIDENCE_MARKER } from '../src/context';
  19. import { isDistinctiveIdentifier } from '../src/search/query-utils';
  20. describe('isDistinctiveIdentifier', () => {
  21. it('treats plain dictionary words as non-distinctive', () => {
  22. for (const word of ['flat', 'object', 'screen', 'standing', 'capture']) {
  23. expect(isDistinctiveIdentifier(word)).toBe(false);
  24. }
  25. });
  26. it('treats leading-capital-only words (proper nouns / sentence start) as non-distinctive', () => {
  27. expect(isDistinctiveIdentifier('Screen')).toBe(false);
  28. expect(isDistinctiveIdentifier('Zustand')).toBe(false);
  29. });
  30. it('treats camelCase / PascalCase / snake_case / acronyms / digits as distinctive', () => {
  31. expect(isDistinctiveIdentifier('setLastEmail')).toBe(true);
  32. expect(isDistinctiveIdentifier('OrgUserStore')).toBe(true);
  33. expect(isDistinctiveIdentifier('user_store')).toBe(true);
  34. expect(isDistinctiveIdentifier('REST')).toBe(true);
  35. expect(isDistinctiveIdentifier('v2')).toBe(true);
  36. });
  37. });
  38. describe('Context ranking — common-word precision & confidence', () => {
  39. let testDir: string;
  40. let cg: CodeGraph;
  41. beforeEach(async () => {
  42. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-ctxrank-'));
  43. // The corroborated target: a capture-flow screen whose NAME alone matches
  44. // three query terms (capture + intro + screen), and which lives under a
  45. // matching directory.
  46. const captureDir = path.join(testDir, 'src', 'app', 'capture');
  47. fs.mkdirSync(captureDir, { recursive: true });
  48. fs.writeFileSync(
  49. path.join(captureDir, 'intro.tsx'),
  50. `export function CaptureIntroScreen() {
  51. // Onboarding screen shown before the user selects flat or standing object capture.
  52. return null;
  53. }
  54. `
  55. );
  56. // The trap: an unrelated constant literally named FLAT, in a totally
  57. // different area. "flat" in a prose query exact-matches it.
  58. const scriptsDir = path.join(testDir, 'scripts', 'dataset');
  59. fs.mkdirSync(scriptsDir, { recursive: true });
  60. fs.writeFileSync(
  61. path.join(scriptsDir, 'download.ts'),
  62. `export const FLAT = 'freiburg_flat_dataset';
  63. export function downloadDataset(name: string): string { return name; }
  64. `
  65. );
  66. cg = CodeGraph.initSync(testDir, {
  67. config: { include: ['**/*.ts', '**/*.tsx'], exclude: [] },
  68. });
  69. await cg.indexAll();
  70. });
  71. afterEach(() => {
  72. if (cg) cg.destroy();
  73. if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
  74. });
  75. it('does not let a common-word exact match (FLAT) outrank a corroborated symbol', async () => {
  76. const sg = await cg.findRelevantContext(
  77. 'capture intro onboarding screen flat object'
  78. );
  79. const rootNames = sg.roots.map((id) => sg.nodes.get(id)?.name);
  80. // The corroborated capture screen surfaces as an entry point...
  81. expect(rootNames).toContain('CaptureIntroScreen');
  82. // ...and the trap constant is never the lead result (the bug we fixed).
  83. expect(rootNames[0]).not.toBe('FLAT');
  84. const capIdx = rootNames.indexOf('CaptureIntroScreen');
  85. const flatIdx = rootNames.indexOf('FLAT');
  86. if (flatIdx >= 0) expect(capIdx).toBeLessThan(flatIdx);
  87. // And it's confidently answered (we located a corroborated symbol).
  88. expect(sg.confidence).toBe('high');
  89. });
  90. it('flags low confidence and emits the handoff when only common words match', async () => {
  91. const query = 'flat object thing';
  92. const sg = await cg.findRelevantContext(query);
  93. expect(sg.confidence).toBe('low');
  94. const md = await cg.buildContext(query, { format: 'markdown' });
  95. expect(typeof md).toBe('string');
  96. expect(md as string).toContain(LOW_CONFIDENCE_MARKER);
  97. // The handoff routes to the precise tools rather than claiming completeness.
  98. expect(md as string).toMatch(/codegraph_explore/);
  99. });
  100. it('does not emit the handoff for a precise, distinctive-symbol query', async () => {
  101. const sg = await cg.findRelevantContext('CaptureIntroScreen');
  102. expect(sg.confidence).toBe('high');
  103. const md = await cg.buildContext('CaptureIntroScreen', { format: 'markdown' });
  104. expect(md as string).not.toContain(LOW_CONFIDENCE_MARKER);
  105. });
  106. });