MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / selectionContext

Function selectionContext

graph-ui/src/why/selection-context.ts:22–84  ·  view source on GitHub ↗
(graph: GraphData | undefined, selected: GraphNode | undefined,
    path: string, agents?: AgentsState, options: { allRelations?: boolean } = {})

Source from the content-addressed store, hash-verified

20
21/** All sentences are derived from edges or observed events. No intent inference. */
22export function selectionContext(graph: GraphData | undefined, selected: GraphNode | undefined,
23 path: string, agents?: AgentsState, options: { allRelations?: boolean } = {}): SelectionContext {
24 const result: SelectionContext = { selected, incoming: [], outgoing: [], incomingByType: [], outgoingByType: [], entryPath: [], pathSearchLimited: false, activity: [] };
25 // A selected node without a file (a Branch node, round 4, N1) still has its relationships; only a file has activity.
26 if (!path && !selected) return result;
27 for (const actor of path ? agents?.actors ?? [] : []) {
28 if (actor.you) continue;
29 for (const event of actor.events) {
30 if (event.path === path) result.activity.push({ agent: actor.name, event });
31 }
32 }
33 result.activity.sort((a, b) => b.event.ts - a.event.ts || b.event.seq - a.event.seq);
34 result.activity = result.activity.slice(0, 8);
35 if (!graph) return result;
36 const nodes = new Map(graph.nodes.map(node => [node.id, node]));
37 // Reindexing may recycle numeric IDs. Resolve the stable source identity
38 // against this snapshot before attributing any relationship to a selection.
39 if (selected) {
40 selected = graph.nodes.find(candidate => selected!.qualified_name
41 ? candidate.qualified_name === selected!.qualified_name && (candidate.file_path ?? '') === path
42 : candidate.id === selected!.id && candidate.name === selected!.name && (candidate.file_path ?? '') === path);
43 result.selected = selected;
44 if (!selected) return result;
45 }
46 const incoming = new Map<number, MapEvidence[]>();
47 const seen = new Set<string>();
48 for (const edge of graph.edges) {
49 if (!options.allRelations && !MAP_RELATIONS.includes(edge.type as MapRelation)) continue;
50 const source = nodes.get(edge.source); const target = nodes.get(edge.target);
51 if (!source || !target) continue;
52 const key = `${source.id}:${edge.type}:${target.id}`;
53 if (seen.has(key)) continue;
54 seen.add(key);
55 const row = { source, target, type: edge.type, id: edge.id, line: edge.line };
56 if (selected ? target.id === selected.id : target.file_path === path && source.file_path !== path) result.incoming.push(row);
57 if (selected ? source.id === selected.id : source.file_path === path && target.file_path !== path) result.outgoing.push(row);
58 if (edge.type === 'CALLS') {
59 const rows = incoming.get(target.id) ?? [];
60 rows.push({ ...row, type: 'CALLS' }); incoming.set(target.id, rows);
61 }
62 }
63 result.incomingByType = countByType(result.incoming);
64 result.outgoingByType = countByType(result.outgoing);
65 // A shortest static caller path within a stated bound. No import edge may
66 // appear in a call path, and no topological ordering is called execution.
67 if (selected && selected.status !== 'entry') {
68 const queue: { id: number; edges: MapEvidence[] }[] = [{ id: selected.id, edges: [] }];
69 const visited = new Set([selected.id]);
70 for (let i = 0; i < queue.length && i < 500; i++) {
71 const current = queue[i];
72 if (current.edges.length >= 4) { if (incoming.get(current.id)?.length) result.pathSearchLimited = true; continue; }
73 for (const edge of incoming.get(current.id) ?? []) {
74 if (visited.has(edge.source.id)) continue;
75 visited.add(edge.source.id);
76 const edges = [edge, ...current.edges];
77 if (edge.source.status === 'entry') { result.entryPath = edges; return result; }
78 if (queue.length < 500) queue.push({ id: edge.source.id, edges });
79 else result.pathSearchLimited = true;

Callers 2

SelectionContextPanelFunction · 0.90

Calls 3

countByTypeFunction · 0.85
getMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected