Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

7.2 KB
Raw Blame
import { LANGUAGE_COLORS, languageColor } from "../../lib/confidence";

// Re-export for convenience
export { LANGUAGE_COLORS, languageColor };

// Community colors are no longer a static jewel-tone array. They live as the
// warm `--color-community-*` token pairs in styles/globals.css and are resolved
// at runtime (per theme) by getCommunityFamily in shared/use-theme-tokens.ts.
// use-sigma's color effect applies them so they track light/dark.

// ---- Node base sizes ----

export const NODE_BASE_SIZES = {
  module: 16,
  file: 6,
  entryPoint: 10,
  test: 4,
} as const;

/**
 * Adaptive node sizing — scales down for large graphs to prevent overlap.
 */
export function getScaledNodeSize(baseSize: number, nodeCount: number): number {
  if (nodeCount > 10000) return Math.max(1, baseSize * 0.4);
  if (nodeCount > 5000) return Math.max(1.5, baseSize * 0.5);
  if (nodeCount > 2000) return Math.max(2, baseSize * 0.65);
  if (nodeCount > 500) return Math.max(2.5, baseSize * 0.8);
  return baseSize;
}

/**
 * FA2 mass by node type — modules are heavier to create clear spatial separation.
 */
export function getNodeMass(
  nodeType: "file" | "module",
  nodeCount: number,
): number {
  const baseMassMultiplier = nodeCount > 5000 ? 2 : nodeCount > 1000 ? 1.5 : 1;
  return nodeType === "module"
    ? 5 * baseMassMultiplier
    : 3 * baseMassMultiplier;
}

// ---- Edge colors by semantic type (warm theme palette) ----
//
// Per-theme literal hex (canvas can't resolve var()); mirrors the semantic
// edge palette in lib/confidence.ts EDGE_COLORS. No "import" key: that kind
// was only ever returned for an edge with an endpoint missing from the graph,
// and the adapter drops those before drawing.
// crossCommunity = plum (--color-accent-secondary), internal = sage/green
// (--color-success family). Dark variants are lifted to read on the near-black
// plum canvas. Resolve via edgeColorsForTheme(); kept literal + allowlisted.

export type EdgeKind =
  | "crossCommunity"
  | "internal"
  | "dynamic"
  | "lowConfidence";

export const EDGE_COLORS_BY_THEME: Record<
  "light" | "dark",
  Record<EdgeKind, string>
> = {
  light: {
    crossCommunity: "#58436c", // plum (accent-secondary)
    internal: "#1d8155", // sage/green (success)
    dynamic: "#8c7f88", // muted text-tertiary
    lowConfidence: "#b8aeb3", // faint plum-gray
  },
  dark: {
    crossCommunity: "#a98fc4", // plum-300 (accent-secondary dark)
    internal: "#34d399", // green (success dark)
    dynamic: "#786f84", // muted text-tertiary dark
    lowConfidence: "#544c5e", // faint plum-gray dark
  },
};

export function edgeColorsForTheme(
  theme: "light" | "dark",
): Record<EdgeKind, string> {
  return EDGE_COLORS_BY_THEME[theme] ?? EDGE_COLORS_BY_THEME.dark;
}

/**
 * Default edge palette for build-time adapter use before the theme-aware
 * recolor effect runs. Dark is the product default theme.
 */
export const EDGE_COLORS = EDGE_COLORS_BY_THEME.dark;

export const EDGE_SIZE_MULTIPLIERS = {
  crossCommunity: 0.8,
  internal: 0.4,
  dynamic: 0.3,
  lowConfidence: 0.3,
} as const;

// ---- ForceAtlas2 adaptive settings ----

export function getFA2Settings(nodeCount: number): Record<string, unknown> {
  const isSmall = nodeCount < 500;
  const isMedium = nodeCount >= 500 && nodeCount < 2000;
  const isLarge = nodeCount >= 2000 && nodeCount < 10000;

  return {
    gravity: isSmall ? 0.8 : isMedium ? 0.5 : isLarge ? 0.3 : 0.2,
    scalingRatio: isSmall ? 12 : isMedium ? 20 : isLarge ? 30 : 40,
    slowDown: isSmall ? 10 : isMedium ? 12 : isLarge ? 15 : 20,
    barnesHutOptimize: nodeCount > 200,
    barnesHutTheta: isLarge ? 0.8 : 0.6,
    strongGravityMode: false,
    outboundAttractionDistribution: true,
    linLogMode: false,
    adjustSizes: true,
    edgeWeightInfluence: 1,
  };
}

/**
 * How long FA2 should run before stopping (ms).
 */
export function getLayoutDuration(nodeCount: number): number {
  if (nodeCount > 2000) return 12000;
  return 8000;
}

// ---- Community seeding (file graphs) ----

/**
 * Diameter of a community's seeding disc, per sqrt(member count), in graph
 * units. Scaling with sqrt keeps node density inside a community roughly
 * constant: the old fixed `sqrt(nodeCount) * 3` gave a 776-file community and a
 * 5-file one the same box, which is why 65% of discs overlapped something on
 * first paint.
 *
 * Measured on this repo's real 1,500-node export, at k=20 with a disc
 * distribution: 20.4% of nodes overlapping (was 65.3%) at cluster separation
 * 20.8 (was 23.2). A square box scores marginally better on separation at equal
 * overlap, but draws communities as squares — the whole page reads in circles.
 */
export const SEED_JITTER_PER_SQRT_MEMBER = 20;

// ---- Synchronous pre-settle (module graphs: settle before first paint) ----

/** Above this, settling synchronously would risk a main-thread jank — the
 *  animated FA2 worker takes over instead. Applies to module graphs only; file
 *  graphs ship their seed as the final layout (see buildFileGraph). */
export const PRESETTLE_MAX_NODES = 800;

/** Sync FA2 iteration budget, scaled down as graphs grow. ~400 iterations on
 *  a 100-node module graph runs in a few ms; 120 on an 800-node expansion
 *  stays well under a frame budget with Barnes-Hut on. */
export function getPresettleIterations(nodeCount: number): number {
  if (nodeCount <= 150) return 400;
  if (nodeCount <= 400) return 250;
  return 120;
}

// ---- Noverlap post-layout settings ----

export const NOVERLAP_SETTINGS = {
  maxIterations: 20,
  ratio: 1.1,
  margin: 10,
  expansion: 1.05,
} as const;

// ---- Edge rendering thresholds ----

/**
 * Above this many EDGES, drop the per-edge curvature and draw straight arrows.
 *
 * This was compared against the node count, not the edge count, while governing
 * how edges draw — so with the export capped at 1,500 nodes it could never fire
 * and every edge drew as a curved arrow. Now measured against what it actually
 * costs: `curvedArrow` carries a per-edge curvature and a heavier program than
 * `arrow`.
 *
 * The straight variant is "arrow", never "line" — `line` has no arrowhead, and
 * direction is the whole point of a dependency edge (#1145 added those heads
 * deliberately).
 */
export const CURVED_EDGE_THRESHOLD = 8000;

// ---- Label rendering ----

export const LABEL_FONT = "JetBrains Mono, ui-monospace, monospace";
export const LABEL_SIZE = 11;
export const LABEL_DENSITY = 0.15;
export const LABEL_GRID_CELL_SIZE = 80;
export const LABEL_RENDERED_SIZE_THRESHOLD = 6;

/**
 * Sparser labels on large graphs to keep repaint cheap.
 *
 * Both thresholds used to step at 2,000 nodes while the export was capped at
 * 1,500 — so neither could ever fire, and the "large graph" branch was dead on
 * every repo. They step at 1,200 now, which is inside the default page, and the
 * ladder has a second rung so a "load more" to the 3,000 ceiling still thins
 * out rather than jumping straight to the densest setting it has.
 */
export function getLabelDensity(nodeCount: number): number {
  if (nodeCount > 2500) return 0.05;
  return nodeCount > 1200 ? 0.07 : LABEL_DENSITY;
}

export function getLabelRenderedSizeThreshold(nodeCount: number): number {
  if (nodeCount > 2500) return 10;
  return nodeCount > 1200 ? 8 : LABEL_RENDERED_SIZE_THRESHOLD;
}