mirror of
https://github.com/colbymchenry/codegraph.git
synced 2026-10-25 20:12:11 +08:00
fix(viewer): the routes list's limit counts routes, not one route's calls
The routing manifest has a row per (route, symbol it reaches), and the routes list behind Entry points and the Steps picker folds those rows to one per route. To have rows to fold, it fetched limit * 4 + 1 of them, so an inline handler, which has a row per call in its body, could use up the window by itself, and the routes after it were never fetched. On an Express app with one 24-call inline handler before two routes, limit=3 listed only GET /dense. getRoutingManifest takes a perRoute flag that applies its limit to routes, in the same file and line order, and returns every row of those routes. Without the flag the query and its result are unchanged. Added on top of the contributor's change: the window is still over-fetched, four routes per route shown, because some routes are dropped only after their rows come back (a test or generated handler, an inline route written in a test). A window of exactly limit + 1 routes could hold only those. On hono, whose routes are nearly all registered in test files, Entry points then listed 0 routes instead of main's 28, and the routes list 21 instead of 32. With the over-fetch, every route main lists is still listed at each limit tried (3, 12, 60, 200 and 300), and hono's Entry points list 32. truncated also keeps main's check that the window came back full. Validation: the new limit=3 test fails on main (only GET /dense) and passes here. A project whose test-file routes sort first fails with the PR as first written (it reads as unrouted) and passes here and on main. Routes listed on Wiki.js (132 routes) and realworld-express are identical to main, and on hono they differ only by the additions above. The ui suites and the other route consumers: 34 files, 723 passed, 0 failed, 3 skipped (environment-gated). The changelog line is in docs/viewer-launch-changelog.md, since the viewer is not released yet. Original PR #2370 by @danusha2345. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
committed by
Colby Mchenry
co-authored by
Claude Opus 5.5
parent
2def0a6d6d
commit
33dc2ae72b
+21
-7
@@ -377,6 +377,7 @@ export class QueryBuilder {
|
||||
getTopRouteFile?: SqliteStatement;
|
||||
unseenName?: SqliteStatement;
|
||||
getRoutingManifest?: SqliteStatement;
|
||||
getRoutingManifestPerRoute?: SqliteStatement;
|
||||
insertNameSegment?: SqliteStatement;
|
||||
} = {};
|
||||
|
||||
@@ -1332,8 +1333,12 @@ export class QueryBuilder {
|
||||
* Also returns the file with the most handler endpoints — used as the
|
||||
* "top handler file" to inline source for, so the agent has both the
|
||||
* mapping AND the handler implementations.
|
||||
*
|
||||
* `limit` caps rows, and a route has a row per symbol it reaches. With
|
||||
* `perRoute` it caps routes instead and returns every row of those routes,
|
||||
* so one inline handler's many calls can't take other routes' places.
|
||||
*/
|
||||
getRoutingManifest(limit: number = 40): {
|
||||
getRoutingManifest(limit: number = 40, perRoute: boolean = false): {
|
||||
entries: Array<{
|
||||
url: string;
|
||||
handler: string;
|
||||
@@ -1349,12 +1354,15 @@ export class QueryBuilder {
|
||||
topHandlerFileCount: number;
|
||||
totalRoutes: number;
|
||||
} | null {
|
||||
if (!this.stmts.getRoutingManifest) {
|
||||
const key = perRoute ? 'getRoutingManifestPerRoute' : 'getRoutingManifest';
|
||||
if (!this.stmts[key]) {
|
||||
// Edge kind varies across framework resolvers: Spring/Rails/
|
||||
// Laravel/Drupal emit `references`, Express emits `calls`. Accept
|
||||
// both — the semantic is the same (route → its handler). A screen in
|
||||
// a Vue / Svelte / Astro app is served by a `component`.
|
||||
this.stmts.getRoutingManifest = this.db.prepare(`
|
||||
const serves = `e.kind IN ('references', 'calls')
|
||||
AND h.kind IN ('function', 'method', 'class', 'constant', 'variable', 'component')`;
|
||||
this.stmts[key] = this.db.prepare(`
|
||||
SELECT
|
||||
r.name AS url,
|
||||
r.id AS route_id,
|
||||
@@ -1368,13 +1376,19 @@ export class QueryBuilder {
|
||||
JOIN edges e ON e.source = r.id
|
||||
JOIN nodes h ON e.target = h.id
|
||||
WHERE r.kind = 'route'
|
||||
AND e.kind IN ('references', 'calls')
|
||||
AND h.kind IN ('function', 'method', 'class', 'constant', 'variable', 'component')
|
||||
AND ${serves}
|
||||
${perRoute ? `AND r.id IN (
|
||||
SELECT r2.id FROM nodes r2
|
||||
WHERE r2.kind = 'route'
|
||||
AND EXISTS (SELECT 1 FROM edges e JOIN nodes h ON e.target = h.id WHERE e.source = r2.id AND ${serves})
|
||||
ORDER BY r2.file_path, r2.start_line
|
||||
LIMIT ?
|
||||
)` : ''}
|
||||
ORDER BY r.file_path, r.start_line
|
||||
LIMIT ?
|
||||
${perRoute ? '' : 'LIMIT ?'}
|
||||
`);
|
||||
}
|
||||
const rows = this.stmts.getRoutingManifest.all(limit) as Array<{
|
||||
const rows = this.stmts[key]!.all(limit) as Array<{
|
||||
url: string; route_id: string; route_file: string; route_line: number;
|
||||
handler: string; handler_file: string; handler_line: number; handler_kind: string;
|
||||
}>;
|
||||
|
||||
+2
-2
@@ -2429,7 +2429,7 @@ export class CodeGraph {
|
||||
* via the `references` edge that framework resolvers emit. Returns
|
||||
* null when fewer than 3 valid (non-test) routes exist.
|
||||
*/
|
||||
getRoutingManifest(limit?: number): {
|
||||
getRoutingManifest(limit?: number, perRoute?: boolean): {
|
||||
entries: Array<{
|
||||
url: string;
|
||||
handler: string;
|
||||
@@ -2444,7 +2444,7 @@ export class CodeGraph {
|
||||
topHandlerFileCount: number;
|
||||
totalRoutes: number;
|
||||
} | null {
|
||||
return this.queries.getRoutingManifest(limit);
|
||||
return this.queries.getRoutingManifest(limit, perRoute);
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
|
||||
@@ -90,8 +90,13 @@ export function splitRouteName(url: string): { method: string | null; path: stri
|
||||
*/
|
||||
const MIN_LIMIT = 3;
|
||||
|
||||
/** Manifest rows fetched per route shown, so folding repeats still fills the page. */
|
||||
const ROWS_PER_ROUTE = 4;
|
||||
/**
|
||||
* Routes fetched per route shown. Some routes in the window are dropped only
|
||||
* after their rows come back (a test or generated handler, an inline route
|
||||
* written in a test), so the window is over-fetched for them, or a project
|
||||
* whose first routes are its tests' would list few or none of its own.
|
||||
*/
|
||||
const ROUTES_PER_ROUTE_SHOWN = 4;
|
||||
|
||||
type ManifestRow = NonNullable<ReturnType<CodeGraph['getRoutingManifest']>>['entries'][number];
|
||||
|
||||
@@ -143,9 +148,11 @@ export function buildRoutes(cg: CodeGraph, query: URLSearchParams): WireRoutes {
|
||||
// symbols, or an inline handler whose every call reads as a "handler"
|
||||
// (hono's `GET /stream/text` came back three times, as `streamText`,
|
||||
// `writeln` and `sleep`), repeats. A route has ONE answer to "what serves
|
||||
// this" — route-roots.ts's — so rows are over-fetched and folded onto it.
|
||||
const fetched = limit * ROWS_PER_ROUTE + 1;
|
||||
const manifest = cg.getRoutingManifest(fetched);
|
||||
// this" — route-roots.ts's — so every row of a route is fetched and folded
|
||||
// onto it. The window counts routes, not rows: one inline handler with
|
||||
// dozens of calls must not push the routes after it off the page.
|
||||
const fetched = limit * ROUTES_PER_ROUTE_SHOWN + 1;
|
||||
const manifest = cg.getRoutingManifest(fetched, true);
|
||||
const routeCount = cg.getStats().nodesByKind.route ?? 0;
|
||||
|
||||
if (!manifest) {
|
||||
@@ -161,7 +168,7 @@ export function buildRoutes(cg: CodeGraph, query: URLSearchParams): WireRoutes {
|
||||
}
|
||||
|
||||
const folded = oneRowPerRoute(cg, manifest.entries);
|
||||
const truncated = folded.length > limit || manifest.entries.length >= fetched;
|
||||
const truncated = folded.length > limit || new Set(manifest.entries.map((e) => e.routeId)).size >= fetched;
|
||||
const rows = folded.slice(0, limit);
|
||||
|
||||
// Every row's handler file in one batched query, so a project that scatters
|
||||
|
||||
Reference in New Issue
Block a user