1
0

adaptive-explore-sizing.test.ts 16 KB

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