Skip to content

Commit d9d7065

Browse files
committed
fix(ui): cap graph rendering and index spawn
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 62a28fd commit d9d7065

13 files changed

Lines changed: 429 additions & 112 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { describe, expect, it } from "vitest";
2+
import { GRAPH_CANVAS_DPR } from "./GraphScene";
3+
4+
describe("GraphScene render limits", () => {
5+
it("caps the high-DPI WebGL backing store below the MSAA failure range", () => {
6+
expect(GRAPH_CANVAS_DPR[0]).toBe(1);
7+
expect(GRAPH_CANVAS_DPR[1]).toBeLessThanOrEqual(1.5);
8+
});
9+
});

‎graph-ui/src/components/GraphScene.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ function CameraAnimator({ target }: { target: CameraTarget | null }) {
4545
/* ── Idle auto-rotation ──────────────────────────────────── */
4646

4747
const IDLE_TIMEOUT_MS = 60_000;
48+
export const GRAPH_CANVAS_DPR: [number, number] = [1, 1.5];
49+
export const GRAPH_COMPOSER_MULTISAMPLING = 0;
4850

4951
function IdleAutoRotate({
5052
controlsRef,
@@ -108,8 +110,12 @@ export function GraphScene({
108110
<Canvas
109111
camera={{ position: [0, 0, 800], fov: 50, near: 0.1, far: 100000 }}
110112
style={{ background: "#06090f" }}
111-
dpr={[1, 2]}
112-
gl={{ antialias: true, alpha: false }}
113+
dpr={GRAPH_CANVAS_DPR}
114+
gl={{
115+
antialias: false,
116+
alpha: false,
117+
powerPreference: "high-performance",
118+
}}
113119
>
114120
<color attach="background" args={["#06090f"]} />
115121
<ambientLight intensity={0.5} />
@@ -176,7 +182,7 @@ export function GraphScene({
176182
<CameraAnimator target={cameraTarget} />
177183
<IdleAutoRotate controlsRef={controlsRef} />
178184

179-
<EffectComposer>
185+
<EffectComposer multisampling={GRAPH_COMPOSER_MULTISAMPLING}>
180186
<Bloom
181187
luminanceThreshold={0.3}
182188
luminanceSmoothing={0.7}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, it } from "vitest";
2+
import { formatGraphLimitNotice } from "./GraphTab";
3+
import type { GraphData } from "../lib/types";
4+
5+
describe("formatGraphLimitNotice", () => {
6+
it("reports when the graph response is truncated for render safety", () => {
7+
const data = {
8+
nodes: Array.from({ length: 2000 }, (_, id) => ({
9+
id,
10+
x: 0,
11+
y: 0,
12+
z: 0,
13+
label: "Function",
14+
name: `fn${id}`,
15+
size: 1,
16+
color: "#ffffff",
17+
})),
18+
edges: [],
19+
total_nodes: 43729,
20+
} satisfies GraphData;
21+
22+
expect(formatGraphLimitNotice(data)).toBe(
23+
"Showing 2,000 of 43,729 nodes. Use filters to narrow.",
24+
);
25+
});
26+
27+
it("stays quiet when the full graph is rendered", () => {
28+
const data = {
29+
nodes: [],
30+
edges: [],
31+
total_nodes: 0,
32+
} satisfies GraphData;
33+
34+
expect(formatGraphLimitNotice(data)).toBeNull();
35+
});
36+
});

‎graph-ui/src/components/GraphTab.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ interface GraphTabProps {
2929
project: string | null;
3030
}
3131

32+
export function formatGraphLimitNotice(data: GraphData | null): string | null {
33+
if (!data || data.total_nodes <= data.nodes.length) return null;
34+
return `Showing ${data.nodes.length.toLocaleString()} of ${data.total_nodes.toLocaleString()} nodes. Use filters to narrow.`;
35+
}
36+
3237
export function GraphTab({ project }: GraphTabProps) {
3338
const { data, loading, error, fetchOverview } = useGraphData();
3439
const [highlightedIds, setHighlightedIds] = useState<Set<number> | null>(null);
@@ -38,6 +43,7 @@ export function GraphTab({ project }: GraphTabProps) {
3843
const [showLabels, setShowLabels] = useState(true);
3944
const [leftWidth, setLeftWidth] = useState(() => loadWidth("cbm-left-w", 260));
4045
const [rightWidth, setRightWidth] = useState(() => loadWidth("cbm-right-w", 280));
46+
const limitNotice = formatGraphLimitNotice(data);
4147

4248
/* Filter state — all enabled by default */
4349
const [enabledLabels, setEnabledLabels] = useState<Set<string>>(new Set());
@@ -282,6 +288,9 @@ export function GraphTab({ project }: GraphTabProps) {
282288
filtered from {data.nodes.length.toLocaleString()}
283289
</p>
284290
)}
291+
{limitNotice && (
292+
<p className="text-amber-300/80 mt-0.5">{limitNotice}</p>
293+
)}
285294
{highlightedIds && highlightedIds.size > 0 && (
286295
<p className="text-cyan-400/50 mt-0.5">
287296
{highlightedIds.size} selected
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { fetchLayout, GRAPH_RENDER_NODE_LIMIT } from "./useGraphData";
3+
4+
describe("fetchLayout", () => {
5+
afterEach(() => {
6+
vi.unstubAllGlobals();
7+
});
8+
9+
it("uses the safe graph render cap by default", async () => {
10+
const fetchMock = vi.fn(async () => ({
11+
ok: true,
12+
json: async () => ({ nodes: [], edges: [], total_nodes: 0 }),
13+
}));
14+
vi.stubGlobal("fetch", fetchMock);
15+
16+
await fetchLayout("large-project");
17+
18+
expect(GRAPH_RENDER_NODE_LIMIT).toBe(2000);
19+
expect(fetchMock).toHaveBeenCalledTimes(1);
20+
const calls = fetchMock.mock.calls as unknown as Array<[string]>;
21+
const [url] = calls[0];
22+
expect(url).toBe(
23+
"/api/layout?project=large-project&max_nodes=2000",
24+
);
25+
});
26+
});

‎graph-ui/src/hooks/useGraphData.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ interface UseGraphDataResult {
99
fetchDetail: (project: string, centerNode: string) => void;
1010
}
1111

12-
async function fetchLayout(
12+
export const GRAPH_RENDER_NODE_LIMIT = 2000;
13+
14+
export async function fetchLayout(
1315
project: string,
14-
maxNodes = 50000,
16+
maxNodes = GRAPH_RENDER_NODE_LIMIT,
1517
): Promise<GraphData> {
1618
const params = new URLSearchParams({ project, max_nodes: String(maxNodes) });
1719
const res = await fetch(`/api/layout?${params}`);
@@ -33,7 +35,7 @@ export function useGraphData(): UseGraphDataResult {
3335
setLoading(true);
3436
setError(null);
3537
try {
36-
const result = await fetchLayout(project, 50000);
38+
const result = await fetchLayout(project);
3739
setData(result);
3840
} catch (e) {
3941
setError(e instanceof Error ? e.message : "Failed to fetch layout");
@@ -48,7 +50,7 @@ export function useGraphData(): UseGraphDataResult {
4850
setError(null);
4951
try {
5052
/* TODO: detail level with center_node filtering */
51-
const result = await fetchLayout(project, 50000);
53+
const result = await fetchLayout(project);
5254
setData(result);
5355
} catch (e) {
5456
setError(e instanceof Error ? e.message : "Failed to fetch layout");

‎graph-ui/tsconfig.tsbuildinfo‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"root":["./src/app.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/api/rpc.ts","./src/components/controltab.tsx","./src/components/edgelines.tsx","./src/components/errorboundary.tsx","./src/components/filterpanel.tsx","./src/components/graphscene.tsx","./src/components/graphtab.tsx","./src/components/nodecloud.tsx","./src/components/nodedetailpanel.tsx","./src/components/nodelabels.tsx","./src/components/nodetooltip.tsx","./src/components/projectcard.tsx","./src/components/resizehandle.tsx","./src/components/sidebar.tsx","./src/components/statstab.tsx","./src/components/tabbar.tsx","./src/components/ui/badge.tsx","./src/components/ui/button.tsx","./src/components/ui/card.tsx","./src/components/ui/checkbox.tsx","./src/components/ui/input.tsx","./src/components/ui/scroll-area.tsx","./src/components/ui/separator.tsx","./src/hooks/usegraphdata.ts","./src/hooks/useprojects.ts","./src/lib/colors.ts","./src/lib/types.ts","./src/lib/utils.ts"],"version":"5.9.3"}
1+
{"root":["./src/app.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/api/rpc.ts","./src/components/controltab.tsx","./src/components/edgelines.tsx","./src/components/errorboundary.tsx","./src/components/filterpanel.tsx","./src/components/graphscene.test.ts","./src/components/graphscene.tsx","./src/components/graphtab.test.ts","./src/components/graphtab.tsx","./src/components/nodecloud.tsx","./src/components/nodedetailpanel.tsx","./src/components/nodelabels.tsx","./src/components/nodetooltip.tsx","./src/components/projectcard.tsx","./src/components/resizehandle.tsx","./src/components/sidebar.tsx","./src/components/statstab.tsx","./src/components/tabbar.tsx","./src/components/ui/badge.tsx","./src/components/ui/button.tsx","./src/components/ui/card.tsx","./src/components/ui/checkbox.tsx","./src/components/ui/input.tsx","./src/components/ui/scroll-area.tsx","./src/components/ui/separator.tsx","./src/hooks/usegraphdata.test.ts","./src/hooks/usegraphdata.ts","./src/hooks/useprojects.ts","./src/lib/colors.ts","./src/lib/types.ts","./src/lib/utils.ts"],"version":"5.9.3"}

‎src/ui/http_server.c‎

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "foundation/log.h"
2424
#include "foundation/platform.h"
2525
#include "foundation/compat.h"
26+
#include "foundation/compat_fs.h"
2627
#include "foundation/str_util.h"
2728
#include "foundation/compat_thread.h"
2829

@@ -39,6 +40,7 @@
3940
#include <process.h>
4041
#include <psapi.h> /* GetProcessMemoryInfo */
4142
#else
43+
#include <sys/stat.h>
4244
#include <unistd.h>
4345
#include <sys/wait.h>
4446
#endif
@@ -582,9 +584,107 @@ static void handle_adr_save(cbm_http_conn_t *c, const cbm_http_req_t *req) {
582584

583585
static char g_binary_path[1024] = {0};
584586

587+
static bool copy_path(char *out, size_t outsz, const char *path) {
588+
if (!out || outsz == 0 || !path || !path[0]) {
589+
return false;
590+
}
591+
int n = snprintf(out, outsz, "%s", path);
592+
return n > 0 && (size_t)n < outsz;
593+
}
594+
595+
#ifndef _WIN32
596+
static bool is_executable_file(const char *path) {
597+
struct stat st;
598+
return path && stat(path, &st) == 0 && S_ISREG(st.st_mode) && access(path, X_OK) == 0;
599+
}
600+
601+
static bool resolve_from_path(const char *name, char *out, size_t outsz) {
602+
const char *path = getenv("PATH");
603+
if (!name || !name[0] || strchr(name, '/') || !path || !path[0]) {
604+
return false;
605+
}
606+
607+
const char *cur = path;
608+
while (*cur) {
609+
const char *colon = strchr(cur, ':');
610+
size_t dir_len = colon ? (size_t)(colon - cur) : strlen(cur);
611+
if (dir_len > 0 && dir_len < 900) {
612+
char candidate[1024];
613+
int n = snprintf(candidate, sizeof(candidate), "%.*s/%s", (int)dir_len, cur, name);
614+
if (n > 0 && (size_t)n < sizeof(candidate) && is_executable_file(candidate)) {
615+
return copy_path(out, outsz, candidate);
616+
}
617+
}
618+
if (!colon) {
619+
break;
620+
}
621+
cur = colon + 1;
622+
}
623+
return false;
624+
}
625+
626+
static bool resolve_self_executable(char *out, size_t outsz) {
627+
#if defined(__APPLE__)
628+
char buf[1024];
629+
uint32_t sz = sizeof(buf);
630+
if (_NSGetExecutablePath(buf, &sz) == 0 && buf[0]) {
631+
return copy_path(out, outsz, buf);
632+
}
633+
return false;
634+
#else
635+
char buf[1024];
636+
ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
637+
if (len > 0) {
638+
buf[len] = '\0';
639+
return copy_path(out, outsz, buf);
640+
}
641+
return false;
642+
#endif
643+
}
644+
#else
645+
static bool resolve_self_executable(char *out, size_t outsz) {
646+
char buf[1024];
647+
DWORD n = GetModuleFileNameA(NULL, buf, (DWORD)sizeof(buf));
648+
if (n > 0 && n < sizeof(buf)) {
649+
return copy_path(out, outsz, buf);
650+
}
651+
return false;
652+
}
653+
#endif
654+
655+
bool cbm_http_server_resolve_binary_path(const char *argv0, char *out, size_t outsz) {
656+
if (!out || outsz == 0) {
657+
return false;
658+
}
659+
out[0] = '\0';
660+
661+
#ifndef _WIN32
662+
if (argv0 && strchr(argv0, '/') && is_executable_file(argv0)) {
663+
return copy_path(out, outsz, argv0);
664+
}
665+
if (resolve_from_path(argv0, out, outsz)) {
666+
return true;
667+
}
668+
#else
669+
if (argv0 && argv0[0]) {
670+
DWORD attrs = GetFileAttributesA(argv0);
671+
if (attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
672+
return copy_path(out, outsz, argv0);
673+
}
674+
}
675+
#endif
676+
677+
if (resolve_self_executable(out, outsz)) {
678+
return true;
679+
}
680+
return copy_path(out, outsz, argv0);
681+
}
682+
585683
void cbm_http_server_set_binary_path(const char *path) {
586684
if (path) {
587-
snprintf(g_binary_path, sizeof(g_binary_path), "%s", path);
685+
if (!cbm_http_server_resolve_binary_path(path, g_binary_path, sizeof(g_binary_path))) {
686+
g_binary_path[0] = '\0';
687+
}
588688
}
589689
}
590690

@@ -597,16 +697,7 @@ static void *index_thread_fn(void *arg) {
597697
const char *bin = g_binary_path;
598698
char self_path[1024] = {0};
599699
if (!bin[0]) {
600-
#ifdef _WIN32
601-
GetModuleFileNameA(NULL, self_path, sizeof(self_path));
602-
#elif defined(__APPLE__)
603-
uint32_t sz = sizeof(self_path);
604-
_NSGetExecutablePath(self_path, &sz);
605-
#else
606-
ssize_t len = readlink("/proc/self/exe", self_path, sizeof(self_path) - 1);
607-
if (len > 0)
608-
self_path[len] = '\0';
609-
#endif
700+
cbm_http_server_resolve_binary_path(NULL, self_path, sizeof(self_path));
610701
bin = self_path[0] ? self_path : "codebase-memory-mcp";
611702
}
612703

‎src/ui/http_server.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define CBM_UI_HTTP_SERVER_H
1212

1313
#include <stdbool.h>
14+
#include <stddef.h>
1415

1516
typedef struct cbm_http_server cbm_http_server_t;
1617

@@ -47,4 +48,7 @@ void cbm_ui_log_append(const char *line);
4748
/* Set the binary path for subprocess spawning (call from main). */
4849
void cbm_http_server_set_binary_path(const char *path);
4950

51+
/* Resolve argv[0] into an executable path suitable for subprocess spawning. */
52+
bool cbm_http_server_resolve_binary_path(const char *argv0, char *out, size_t outsz);
53+
5054
#endif /* CBM_UI_HTTP_SERVER_H */

‎src/ui/layout3d.c‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
#include <yyjson/yyjson.h>
1818

1919
#include <math.h>
20+
#include <errno.h>
2021
#include <stdlib.h>
2122
#include <string.h>
2223
#include <stdint.h>
2324

2425
/* ── Constants ────────────────────────────────────────────────── */
2526

26-
#define DEFAULT_MAX_NODES 50000
27+
#define DEFAULT_MAX_NODES 2000
28+
#define HARD_MAX_NODES 10000
2729
#define BH_THETA 1.2f
2830

2931
/* Local optimization: gentle, preserves structure */
@@ -110,6 +112,31 @@ static float rand_float(uint32_t *seed) {
110112
return (float)((*seed >> 16) & 0x7FFF) / 32768.0f - 0.5f;
111113
}
112114

115+
static int render_node_limit(void) {
116+
const char *raw = getenv("CBM_UI_MAX_RENDER_NODES");
117+
if (!raw || !raw[0]) {
118+
return DEFAULT_MAX_NODES;
119+
}
120+
errno = 0;
121+
char *end = NULL;
122+
long v = strtol(raw, &end, 10);
123+
if (errno != 0 || end == raw || *end != '\0' || v <= 0) {
124+
return DEFAULT_MAX_NODES;
125+
}
126+
if (v > HARD_MAX_NODES) {
127+
return HARD_MAX_NODES;
128+
}
129+
return (int)v;
130+
}
131+
132+
static int clamp_max_nodes(int requested) {
133+
int cap = render_node_limit();
134+
if (requested <= 0 || requested > cap) {
135+
return cap;
136+
}
137+
return requested;
138+
}
139+
113140
/* ── Barnes-Hut Octree ────────────────────────────────────────── */
114141

115142
typedef struct octree_node {
@@ -387,8 +414,7 @@ cbm_layout_result_t *cbm_layout_compute(cbm_store_t *store, const char *project,
387414
int radius, int max_nodes) {
388415
if (!store || !project)
389416
return NULL;
390-
if (max_nodes <= 0)
391-
max_nodes = DEFAULT_MAX_NODES;
417+
max_nodes = clamp_max_nodes(max_nodes);
392418
(void)center_node;
393419
(void)radius;
394420
(void)level;

0 commit comments

Comments
 (0)