1
0

adaptive-explore-sizing.test.ts 16 KB

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