adaptive-explore-sizing.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * Regression test for adaptive `codegraph_explore` sizing — sibling
  3. * skeletonization (branch `feat/adaptive-explore-sizing`, commit d6d059f).
  4. *
  5. * Feature: when a file is BOTH (1) off the synthesized flow spine AND (2) a
  6. * polymorphic sibling — its class implements/extends a supertype shared by
  7. * >= MIN_SIBLINGS (3) implementers — `codegraph_explore` renders it as a
  8. * class + member *signature* skeleton (bodies elided) instead of full source,
  9. * keeping the on-spine exemplar and the mechanism full. This sizes the
  10. * response to the answer rather than the budget cap on sibling-heavy flows
  11. * (OkHttp's interceptor chain) without starving diffuse ones (distinct
  12. * pipeline steps stay full). Default ON; CODEGRAPH_ADAPTIVE_EXPLORE=0 disables.
  13. *
  14. * The fixture is OkHttp's interceptor chain in miniature:
  15. * - `Interceptor` interface with FOUR implementers (>= 3 => a sibling family)
  16. * - a 3-hop call spine `dispatch -> proceed -> handleLogging` that passes
  17. * THROUGH LoggingInterceptor — so that file is the on-spine exemplar
  18. * - Bridge/Cache/RetryInterceptor: off-spine members of the sibling family
  19. * => skeletonize
  20. * - ResponseFormatter implements `Formatter`, which has only ONE impl (< 3)
  21. * => a distinct step: off-spine but NOT a sibling => stays full
  22. *
  23. * Guards the two ways the feature can silently regress: skeletonizing too much
  24. * (a distinct step or the on-spine exemplar) or too little (the off-spine
  25. * siblings), plus the escape hatch.
  26. */
  27. import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
  28. import * as fs from 'fs';
  29. import * as path from 'path';
  30. import * as os from 'os';
  31. import { ToolHandler } from '../src/mcp/tools';
  32. import CodeGraph from '../src/index';
  33. const SKELETON_MARK = '· skeleton (signatures only; Read for a full body)';
  34. /** Return the `#### <path> ...` section for a file basename, header through the
  35. * line before the next `###`/`####` header (or end of output). */
  36. function sectionFor(text: string, basename: string): string {
  37. const lines = text.split('\n');
  38. const start = lines.findIndex((l) => l.startsWith('#### ') && l.includes(basename));
  39. if (start < 0) return '';
  40. let end = lines.length;
  41. for (let i = start + 1; i < lines.length; i++) {
  42. if (lines[i].startsWith('### ') || lines[i].startsWith('#### ')) {
  43. end = i;
  44. break;
  45. }
  46. }
  47. return lines.slice(start, end).join('\n');
  48. }
  49. describe('adaptive codegraph_explore sizing — sibling skeletonization', () => {
  50. let testDir: string;
  51. let cg: CodeGraph;
  52. let handler: ToolHandler;
  53. // Names the spine (dispatch/proceed/handleLogging), the on-spine exemplar,
  54. // the three off-spine siblings, and the distinct step — so every file we
  55. // assert on is gathered as relevant. maxFiles overrides the very-tiny tier's
  56. // 4-file default so all of them land in one call.
  57. const QUERY =
  58. 'dispatch proceed handleLogging LoggingInterceptor BridgeInterceptor CacheInterceptor RetryInterceptor ResponseFormatter';
  59. beforeAll(async () => {
  60. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-adaptive-explore-'));
  61. const srcDir = path.join(testDir, 'src');
  62. fs.mkdirSync(srcDir);
  63. const write = (name: string, body: string) =>
  64. fs.writeFileSync(path.join(srcDir, name), body.trimStart());
  65. // The interchangeable contract — 4 implementers below => sibling family.
  66. write(
  67. 'interceptor.ts',
  68. `
  69. export interface Interceptor {
  70. intercept(request: string): string;
  71. }
  72. `
  73. );
  74. // The mechanism + the spine: dispatch -> proceed -> (LoggingInterceptor) handleLogging.
  75. // Unique method names so the call edges resolve unambiguously.
  76. write(
  77. 'dispatcher.ts',
  78. `
  79. import { LoggingInterceptor } from './logging-interceptor';
  80. export class RequestDispatcher {
  81. dispatch(): string {
  82. const chain = new InterceptorChain();
  83. return chain.proceed();
  84. }
  85. }
  86. export class InterceptorChain {
  87. proceed(): string {
  88. const exemplar = new LoggingInterceptor();
  89. return exemplar.handleLogging();
  90. }
  91. }
  92. `
  93. );
  94. // On-spine exemplar: handleLogging is the spine's tail, so this whole file
  95. // is on-spine and must stay FULL even though it's a sibling (implements Interceptor).
  96. write(
  97. 'logging-interceptor.ts',
  98. `
  99. import { Interceptor } from './interceptor';
  100. export class LoggingInterceptor implements Interceptor {
  101. handleLogging(): string {
  102. const tag = 'LOGGING_BODY_MARKER';
  103. return this.intercept(tag);
  104. }
  105. intercept(request: string): string {
  106. return 'logged:' + request;
  107. }
  108. }
  109. `
  110. );
  111. // Off-spine siblings — interchangeable impls of Interceptor => SKELETONIZE.
  112. // Each body carries a unique marker that must NOT survive skeletonization.
  113. write(
  114. 'bridge-interceptor.ts',
  115. `
  116. import { Interceptor } from './interceptor';
  117. export class BridgeInterceptor implements Interceptor {
  118. intercept(request: string): string {
  119. const detail = 'BRIDGE_BODY_MARKER';
  120. return 'bridged:' + request + detail;
  121. }
  122. }
  123. `
  124. );
  125. write(
  126. 'cache-interceptor.ts',
  127. `
  128. import { Interceptor } from './interceptor';
  129. export class CacheInterceptor implements Interceptor {
  130. intercept(request: string): string {
  131. const detail = 'CACHE_BODY_MARKER';
  132. return 'cached:' + request + detail;
  133. }
  134. }
  135. `
  136. );
  137. write(
  138. 'retry-interceptor.ts',
  139. `
  140. import { Interceptor } from './interceptor';
  141. export class RetryInterceptor implements Interceptor {
  142. intercept(request: string): string {
  143. const detail = 'RETRY_BODY_MARKER';
  144. return 'retried:' + request + detail;
  145. }
  146. }
  147. `
  148. );
  149. // A 1:1 interface->impl pair: off-spine, implements something, but the
  150. // supertype has only ONE impl (< MIN_SIBLINGS) => a DISTINCT step => FULL.
  151. write(
  152. 'formatter.ts',
  153. `
  154. export interface Formatter {
  155. format(input: string): string;
  156. }
  157. `
  158. );
  159. write(
  160. 'response-formatter.ts',
  161. `
  162. import { Formatter } from './formatter';
  163. import { JsonCodec } from './codec';
  164. export class ResponseFormatter implements Formatter {
  165. format(input: string): string {
  166. const detail = 'FORMATTER_BODY_MARKER';
  167. // Calls into the Codec family from OFF the dispatch spine, so codec.ts is
  168. // gathered as relevant but stays off-spine (mirrors Django: compiler.py is
  169. // referenced by the flow yet off the QuerySet-iteration spine).
  170. return new JsonCodec().encode(input) + detail;
  171. }
  172. }
  173. `
  174. );
  175. // An off-spine sibling (implements Interceptor) the agent would otherwise
  176. // skeletonize — BUT it owns a uniquely-named method `authenticate` the agent
  177. // names in the query. Mirrors OkHttp's RealCall (named getResponseWith-
  178. // InterceptorChain): a named callable means "show me this", so it stays full.
  179. write(
  180. 'auth-interceptor.ts',
  181. `
  182. import { Interceptor } from './interceptor';
  183. export class AuthInterceptor implements Interceptor {
  184. authenticate(token: string): string {
  185. const detail = 'AUTH_BODY_MARKER';
  186. return 'auth:' + token + detail;
  187. }
  188. intercept(request: string): string {
  189. return this.authenticate(request);
  190. }
  191. }
  192. `
  193. );
  194. // A base class that DEFINES a >=3-impl supertype AND co-locates its
  195. // subclasses in the same file — mirrors Django's compiler.py (SQLCompiler +
  196. // SQLInsertCompiler/SQLUpdateCompiler/...). The subclasses' `extends` edges
  197. // make the file look like a sibling, but it's the family's base/mechanism,
  198. // so it must stay full.
  199. write(
  200. 'codec.ts',
  201. `
  202. export class Codec {
  203. encode(input: string): string {
  204. const detail = 'CODEC_BASE_MARKER';
  205. return input + detail;
  206. }
  207. }
  208. export class JsonCodec extends Codec {
  209. encode(input: string): string { return '{' + input + '}'; }
  210. }
  211. export class XmlCodec extends Codec {
  212. encode(input: string): string { return '<' + input + '>'; }
  213. }
  214. export class YamlCodec extends Codec {
  215. encode(input: string): string { return '- ' + input; }
  216. }
  217. `
  218. );
  219. cg = CodeGraph.initSync(testDir, { config: { include: ['**/*.ts'], exclude: [] } });
  220. await cg.indexAll();
  221. handler = new ToolHandler(cg);
  222. });
  223. afterAll(() => {
  224. if (cg) cg.destroy();
  225. if (testDir && fs.existsSync(testDir)) {
  226. fs.rmSync(testDir, { recursive: true, force: true });
  227. }
  228. });
  229. beforeEach(() => {
  230. // Each test asserts against the default (ON) behaviour unless it opts out.
  231. delete process.env.CODEGRAPH_ADAPTIVE_EXPLORE;
  232. });
  233. it('fixture sanity: Interceptor has >=3 implementers, Formatter has <3', () => {
  234. const find = (name: string, kind: string) =>
  235. cg.searchNodes(name).map((r) => r.node).find((n) => n.name === name && n.kind === kind);
  236. const interceptor = find('Interceptor', 'interface');
  237. const formatter = find('Formatter', 'interface');
  238. expect(interceptor).toBeTruthy();
  239. expect(formatter).toBeTruthy();
  240. const implementers = (id: string) =>
  241. cg.getIncomingEdges(id).filter((e) => e.kind === 'implements' || e.kind === 'extends').length;
  242. // The whole gate hinges on this signal — assert the fixture actually
  243. // produces the >=3 / <3 split, so a TS-extraction change fails here loudly
  244. // rather than silently flipping the skeletonization downstream.
  245. expect(implementers(interceptor!.id)).toBeGreaterThanOrEqual(3);
  246. expect(implementers(formatter!.id)).toBeLessThan(3);
  247. });
  248. it('skeletonizes off-spine polymorphic siblings (bodies elided, signatures kept)', async () => {
  249. const result = await handler.execute('codegraph_explore', { query: QUERY, maxFiles: 12 });
  250. const text = result.content?.[0]?.text ?? '';
  251. // Precondition: the spine must have formed, or nothing skeletonizes.
  252. expect(text).toContain('## Flow (call path among the symbols you queried)');
  253. for (const [file, marker] of [
  254. ['bridge-interceptor.ts', 'BRIDGE_BODY_MARKER'],
  255. ['cache-interceptor.ts', 'CACHE_BODY_MARKER'],
  256. ['retry-interceptor.ts', 'RETRY_BODY_MARKER'],
  257. ] as const) {
  258. const section = sectionFor(text, file);
  259. expect(section, `${file} should be present in the explore output`).not.toBe('');
  260. expect(section, `${file} should be skeletonized`).toContain(SKELETON_MARK);
  261. // The signature line survives; the body (with its marker) is elided.
  262. expect(section).toContain('intercept(request');
  263. expect(section, `${file} body marker must NOT survive skeletonization`).not.toContain(marker);
  264. }
  265. });
  266. it('keeps the on-spine exemplar full even though it is a sibling', async () => {
  267. const result = await handler.execute('codegraph_explore', { query: QUERY, maxFiles: 12 });
  268. const text = result.content?.[0]?.text ?? '';
  269. const section = sectionFor(text, 'logging-interceptor.ts');
  270. expect(section, 'logging-interceptor.ts should be present').not.toBe('');
  271. expect(section, 'on-spine exemplar must NOT be skeletonized').not.toContain(SKELETON_MARK);
  272. // Full source => the body marker is present.
  273. expect(section).toContain('LOGGING_BODY_MARKER');
  274. });
  275. it('keeps a distinct step full (off-spine but supertype has < 3 implementers)', async () => {
  276. const result = await handler.execute('codegraph_explore', { query: QUERY, maxFiles: 12 });
  277. const text = result.content?.[0]?.text ?? '';
  278. const section = sectionFor(text, 'response-formatter.ts');
  279. expect(section, 'response-formatter.ts should be present').not.toBe('');
  280. expect(section, 'a 1:1 interface impl is not a sibling and must stay full').not.toContain(SKELETON_MARK);
  281. expect(section).toContain('FORMATTER_BODY_MARKER');
  282. });
  283. it('CODEGRAPH_ADAPTIVE_EXPLORE=0 disables skeletonization (siblings render full)', async () => {
  284. process.env.CODEGRAPH_ADAPTIVE_EXPLORE = '0';
  285. try {
  286. const result = await handler.execute('codegraph_explore', { query: QUERY, maxFiles: 12 });
  287. const text = result.content?.[0]?.text ?? '';
  288. expect(text, 'no file should be skeletonized with the flag off').not.toContain(SKELETON_MARK);
  289. // The previously-skeletonized siblings now render their full bodies.
  290. const section = sectionFor(text, 'bridge-interceptor.ts');
  291. expect(section).not.toBe('');
  292. expect(section).toContain('BRIDGE_BODY_MARKER');
  293. } finally {
  294. delete process.env.CODEGRAPH_ADAPTIVE_EXPLORE;
  295. }
  296. });
  297. // Names AuthInterceptor's `authenticate` and Codec's `encode` (both methods),
  298. // plus the spine tokens so a spine still forms. Same Interceptor family as the
  299. // skeleton test, plus the Codec base+subclasses family.
  300. const SPARE_QUERY = `${QUERY} authenticate encode AuthInterceptor Codec JsonCodec`;
  301. it('spares an off-spine sibling when the agent NAMED a callable in it (RealCall fix)', async () => {
  302. const result = await handler.execute('codegraph_explore', { query: SPARE_QUERY, maxFiles: 15 });
  303. const text = result.content?.[0]?.text ?? '';
  304. expect(text).toContain('## Flow (call path among the symbols you queried)');
  305. // auth-interceptor.ts is an off-spine Interceptor sibling — would skeletonize —
  306. // but the agent named its method `authenticate`, so it stays FULL.
  307. const auth = sectionFor(text, 'auth-interceptor.ts');
  308. expect(auth, 'auth-interceptor.ts should be present').not.toBe('');
  309. expect(auth, 'a file holding an agent-named callable must NOT be skeletonized').not.toContain(SKELETON_MARK);
  310. expect(auth).toContain('AUTH_BODY_MARKER');
  311. // Contrast: bridge-interceptor.ts — same family, named only by TYPE — still skeletonizes.
  312. const bridge = sectionFor(text, 'bridge-interceptor.ts');
  313. expect(bridge, 'a sibling named only by type still skeletonizes').toContain(SKELETON_MARK);
  314. expect(bridge).not.toContain('BRIDGE_BODY_MARKER');
  315. });
  316. it('skeletonizes a base+subclasses family file even when named (compiler.py: family override beats the named spare)', async () => {
  317. const result = await handler.execute('codegraph_explore', { query: SPARE_QUERY, maxFiles: 15 });
  318. const text = result.content?.[0]?.text ?? '';
  319. // codec.ts defines the base Codec (>=3 subclasses extend it) and co-locates the
  320. // subclasses — a redundant, Read-anyway "family" file (Django's compiler.py). Even
  321. // though the agent named `encode`, it STILL skeletonizes: a full one would eat the
  322. // explore budget and starve the sibling files. Contrast auth-interceptor.ts above,
  323. // which is named AND not a family file → spared. This is the override that keeps
  324. // Django from regressing (sparing the family file cost more and Read more).
  325. const codec = sectionFor(text, 'codec.ts');
  326. expect(codec, 'codec.ts should be present').not.toBe('');
  327. expect(codec, 'a named base+subclasses family file still skeletonizes (budget)').toContain(SKELETON_MARK);
  328. expect(codec, 'the elided base body marker must NOT survive').not.toContain('CODEC_BASE_MARKER');
  329. });
  330. });