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

Method buildPolymorphicBoundaries

src/mcp/tools.ts:4021–4144  ·  view source on GitHub ↗

* Interface/registry-dispatch announcement — #687 extended to GRAPH-visible * polymorphism (the body-scan can't see it: `nodeType.execute()` is textually * an ordinary call; the polymorphism lives in the `implements`/`extends` edges). * * A method the agent named that resolves to a large

(cg: CodeGraph, candidates: Array<{ token: string; family: Node[] }>, named: Map<string, Node>)

Source from the content-addressed store, hash-verified

4019 * TRUE graph-wide implementer count, NOT their frequency in the sample.
4020 */
4021 private buildPolymorphicBoundaries(cg: CodeGraph, candidates: Array<{ token: string; family: Node[] }>, named: Map<string, Node>): string {
4022 const CLASSY = new Set(['class', 'struct', 'interface', 'trait', 'protocol', 'abstract']);
4023 const MIN_IMPL = 8; // a supertype needs >= this many implementers to count as "polymorphic"
4024 const MIN_SUPPORT = 2; // >= this many sampled definers must share the supertype (ties it to the token)
4025 const SAMPLE = 40; // family members inspected per token
4026 const MAX_NOTES = 3;
4027 const rel = (p: string) => p.replace(/\\/g, '/');
4028 const containerOf = (m: Node): Node | null => {
4029 try { const ce = cg.getIncomingEdges(m.id).find((e) => e.kind === 'contains'); return ce ? cg.getNode(ce.source) : null; }
4030 catch { return null; }
4031 };
4032 // A supertype dispatches only a member it (or an ancestor) declares. Without
4033 // this, any name shared by enough subclasses read as dispatch through their
4034 // common base: on vscode the query word `extension`, a getter on unrelated
4035 // classes that all extend `Disposable`, was announced as "runtime dispatch to
4036 // 2706 types implementing Disposable" at the top of 9 of 31 answers.
4037 //
4038 // A Swift protocol and each of its extensions are separate nodes of one name,
4039 // and a conformer's edge may land on any of them (Alamofire's
4040 // `RequestInterceptor` conformers point at an extension in OfflineRetrier.swift
4041 // that has no `adapt`), so every same-named type is asked. The answer is
4042 // `unknown`, and the announcement stays, whenever absence can't be judged:
4043 // nothing on the chain has indexed members of the family's sort, or an
4044 // interface/protocol on it has none (Swift requirements aren't nodes, so
4045 // `URLRequestConvertible` can't be said to lack `asURLRequest`).
4046 const memberOfSupertype = (typeId: string, name: string, callable: boolean): 'declared' | 'absent' | 'unknown' => {
4047 const counts = (kind: string) => !callable || kind === 'method' || kind === 'function';
4048 let sawMembers = false;
4049 let opaque = false;
4050 const seen = new Set<string>();
4051 const visit = (id: string, depth: number): boolean => {
4052 if (seen.has(id)) return false;
4053 seen.add(id);
4054 let node: Node | null = null;
4055 let edges: ReturnType<typeof cg.getOutgoingEdges> = [];
4056 try { node = cg.getNode(id); edges = cg.getOutgoingEdges(id); } catch { return false; }
4057 let own = 0;
4058 for (const e of edges) {
4059 if (e.kind !== 'contains') continue;
4060 let child: Node | null = null;
4061 try { child = cg.getNode(e.target); } catch { child = null; }
4062 if (!child) continue;
4063 if (child.name === name) return true;
4064 if (counts(child.kind)) own++;
4065 }
4066 if (own > 0) sawMembers = true;
4067 else if (node && (node.kind === 'interface' || node.kind === 'protocol' || node.kind === 'trait')) opaque = true;
4068 if (depth >= 3) return false;
4069 return edges.some((e) => (e.kind === 'extends' || e.kind === 'implements') && visit(e.target, depth + 1));
4070 };
4071 let root: Node | null = null;
4072 try { root = cg.getNode(typeId); } catch { root = null; }
4073 let namesakes: Node[] = [];
4074 try {
4075 namesakes = root
4076 ? cg.getNodesByName(root.name).filter((n) => n.language === root!.language && CLASSY.has(n.kind))
4077 : [];
4078 } catch { namesakes = []; }

Callers 1

Calls 11

countImplementersFunction · 0.90
relFunction · 0.85
hasMethod · 0.80
mapMethod · 0.80
getNodeMethod · 0.80
pushMethod · 0.80
getMethod · 0.65
getOutgoingEdgesMethod · 0.45
setMethod · 0.45
addMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected