MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / explore

Function explore

__tests__/explore-allocation-e2e.test.ts:84–140  ·  view source on GitHub ↗

* One explore call, reduced to what the allocation assertions need — plus the * CG-4 per-file diagnostic, which is where the SCORE and the RESERVATION live. * The instrument is observational (byte-identical output either way), so reading * it here measures the same response the agent would have r

(project: Project, query: string)

Source from the content-addressed store, hash-verified

82 * it here measures the same response the agent would have received.
83 */
84async function explore(project: Project, query: string) {
85 // Outside the project root on purpose: a sidecar written INTO the indexed tree
86 // is a new file the watcher can pick up mid-suite.
87 const sidecar = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-alloc-diag-')), 'report.jsonl');
88 const previous = process.env[DEBUG_ENV];
89 process.env[DEBUG_ENV] = sidecar;
90 // This file is about how the tier's budget is split and spent. A query
91 // anchored on named symbols is now sized to them instead
92 // (explore-named-sizing.test.ts), so the tier's budget is kept in force here.
93 const sizing = process.env.CODEGRAPH_EXPLORE_NAMED_SIZING;
94 process.env.CODEGRAPH_EXPLORE_NAMED_SIZING = '0';
95 let result;
96 try {
97 result = await project.handler.execute('codegraph_explore', { query });
98 } finally {
99 if (previous === undefined) delete process.env[DEBUG_ENV];
100 else process.env[DEBUG_ENV] = previous;
101 if (sizing === undefined) delete process.env.CODEGRAPH_EXPLORE_NAMED_SIZING;
102 else process.env.CODEGRAPH_EXPLORE_NAMED_SIZING = sizing;
103 }
104 const text = result.content?.[0]?.text ?? '';
105 const bytes = attributeSourceBytes(text);
106 const lines = fs.existsSync(sidecar)
107 ? fs.readFileSync(sidecar, 'utf-8').trim().split('\n').filter(Boolean)
108 : [];
109 const report = JSON.parse(lines[lines.length - 1]!) as ExploreDiagnosticReport;
110 fs.rmSync(path.dirname(sidecar), { recursive: true, force: true });
111 const fileOf = (file: string) => report.files.find((f) => f.path === file);
112 return {
113 text,
114 bytes,
115 report,
116 isError: result.isError === true,
117 /** Relevance score the ranking pass gave this file. */
118 score: (file: string) => fileOf(file)?.score ?? 0,
119 /** Chars of source the allocator RESERVED for it, before anything rendered. */
120 allowance: (file: string) => fileOf(file)?.allowance ?? 0,
121 /** Which render path the loop took: `whole`, `clusters`, `focused`, `skeleton`. */
122 render: (file: string) => fileOf(file)?.render ?? null,
123 /**
124 * Rank the ranking pass gave it (1 = the file the response leads with, and
125 * the first the render loop reaches). Read off the record rather than from
126 * the position in `report.files`, which the report re-sorts by delivered
127 * bytes for legibility.
128 */
129 rank: (file: string) => fileOf(file)?.rank ?? -1,
130 /** Total source bytes delivered across every rendered file. */
131 sourceTotal: () => [...bytes.values()].reduce((sum, n) => sum + n, 0),
132 /** Fraction of the WHOLE response this file's source occupies. */
133 share: (file: string) => (bytes.get(file) ?? 0) / (text.length || 1),
134 shareUnder: (prefix: string) => {
135 let total = 0;
136 for (const [file, n] of bytes) if (file.startsWith(prefix)) total += n;
137 return total / (text.length || 1);
138 },
139 };
140}
141

Callers 1

Calls 6

attributeSourceBytesFunction · 0.90
parseMethod · 0.80
fileOfFunction · 0.70
getMethod · 0.65
joinMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected