Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 92 additions & 8 deletions src/functions/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,81 @@ import {
import { recordAudit } from "./audit.js";
import { logger } from "../logger.js";

// #753: keep the response payload below the iii state channel ceiling.
// 500 nodes + their incident edges hold well under the limit on the
// reported 11k-node / 28k-edge corpus, and 5,000 is the upper bound a
// caller can request explicitly. Tuned conservatively because edges
// fan out faster than nodes.
const DEFAULT_GRAPH_QUERY_LIMIT = 500;
const MAX_GRAPH_QUERY_LIMIT = 5000;

function resolvePagination(
rawLimit: number | undefined,
rawOffset: number | undefined,
): { limit: number; offset: number } {
const requested = typeof rawLimit === "number" && Number.isFinite(rawLimit)
? Math.floor(rawLimit)
: DEFAULT_GRAPH_QUERY_LIMIT;
const limit = Math.max(1, Math.min(requested, MAX_GRAPH_QUERY_LIMIT));
const offset = Math.max(
0,
typeof rawOffset === "number" && Number.isFinite(rawOffset)
? Math.floor(rawOffset)
: 0,
);
return { limit, offset };
}

// Score nodes by incident-edge count so the default-cap page surfaces
// the densest part of the graph rather than an arbitrary KV scan order.
function rankByDegree(nodes: GraphNode[], edges: GraphEdge[]): GraphNode[] {
const degree = new Map<string, number>();
for (const edge of edges) {
degree.set(edge.sourceNodeId, (degree.get(edge.sourceNodeId) ?? 0) + 1);
degree.set(edge.targetNodeId, (degree.get(edge.targetNodeId) ?? 0) + 1);
}
return [...nodes].sort((a, b) => (degree.get(b.id) ?? 0) - (degree.get(a.id) ?? 0));
}

function paginate(
nodes: GraphNode[],
allEdges: GraphEdge[],
depth: number,
limit: number,
offset: number,
): GraphQueryResult {
const totalNodes = nodes.length;
const pageNodes = nodes.slice(offset, offset + limit);
const pageNodeIds = new Set(pageNodes.map((n) => n.id));
// Edges restricted to the page so the response payload scales with
// `limit`, not with the global edge count. An edge is included only
// when BOTH endpoints land in the page — half-edges to nodes outside
// the page would render as dangling links in the viewer.
const pageEdges = allEdges.filter(
(e) => pageNodeIds.has(e.sourceNodeId) && pageNodeIds.has(e.targetNodeId),
);
// Total edges (for the same node universe). Counted unbounded so the
// viewer can show "showing X of Y" without re-querying.
const universeIds = new Set(nodes.map((n) => n.id));
const totalEdges = allEdges.reduce(
(count, e) =>
universeIds.has(e.sourceNodeId) && universeIds.has(e.targetNodeId)
? count + 1
: count,
0,
);
return {
nodes: pageNodes,
edges: pageEdges,
depth,
totalNodes,
totalEdges,
truncated: totalNodes > pageNodes.length,
limit,
offset,
};
}

// Parse all key="value" pairs from a tag's attribute string, in any
// order. The previous parser hard-coded attribute order
// (type before name on <entity>, type/source/target/weight on
Expand Down Expand Up @@ -198,16 +273,25 @@ export function registerGraphFunction(
},
);

sdk.registerFunction("mem::graph-query",
// #753: every branch now applies a default cap and reports the
// unbounded `total*` counts. Before this change, an unfiltered POST
// /graph/query body (`{}`) on a corpus with ~10k+ nodes serialized
// to a payload large enough that the iii state response channel
// rejected it with HTTP 500 "Invocation stopped", leaving the viewer
// graph tab silently blank.
sdk.registerFunction("mem::graph-query",
async (data: {
startNodeId?: string;
nodeType?: string;
maxDepth?: number;
query?: string;
limit?: number;
offset?: number;
}): Promise<GraphQueryResult> => {
const allNodes = (await kv.list<GraphNode>(KV.graphNodes)).filter((n) => !n.stale);
const allEdges = (await kv.list<GraphEdge>(KV.graphEdges)).filter((e) => !e.stale);
const maxDepth = Math.min(data.maxDepth || 3, 5);
const { limit, offset } = resolvePagination(data.limit, data.offset);

if (data.query) {
const lower = data.query.toLowerCase();
Expand All @@ -218,11 +302,7 @@ export function registerGraphFunction(
(v) => typeof v === "string" && v.toLowerCase().includes(lower),
),
);
const nodeIds = new Set(matchingNodes.map((n) => n.id));
const relatedEdges = allEdges.filter(
(e) => nodeIds.has(e.sourceNodeId) || nodeIds.has(e.targetNodeId),
);
return { nodes: matchingNodes, edges: relatedEdges, depth: 0 };
return paginate(matchingNodes, allEdges, 0, limit, offset);
}

if (data.startNodeId) {
Expand Down Expand Up @@ -264,14 +344,18 @@ export function registerGraphFunction(
}
}

return { nodes: resultNodes, edges: resultEdges, depth: maxDepth };
return paginate(resultNodes, resultEdges, maxDepth, limit, offset);
}

let filtered = allNodes;
if (data.nodeType) {
filtered = allNodes.filter((n) => n.type === data.nodeType);
}
return { nodes: filtered, edges: allEdges, depth: 0 };
// Empty-body / nodeType-only branch is the path the viewer hits
// on tab load. Page by the most-connected nodes first so the
// truncated view conveys the densest part of the graph (#753).
const ranked = rankByDegree(filtered, allEdges);
return paginate(ranked, allEdges, 0, limit, offset);
},
);

Expand Down
16 changes: 14 additions & 2 deletions src/triggers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,19 +1330,31 @@ export function registerApiTriggers(
},
});

sdk.registerFunction("api::graph-query",
sdk.registerFunction("api::graph-query",
async (
req: ApiRequest<{
startNodeId?: string;
nodeType?: string;
maxDepth?: number;
query?: string;
limit?: number;
offset?: number;
}>,
): Promise<Response> => {
const authErr = checkAuth(req, secret);
if (authErr) return authErr;
// Whitelist payload fields explicitly; AGENTS.md security rule:
// REST endpoints never pass raw req.body through to sdk.trigger.
const payload = {
startNodeId: req.body?.startNodeId,
nodeType: req.body?.nodeType,
maxDepth: req.body?.maxDepth,
query: req.body?.query,
limit: req.body?.limit,
offset: req.body?.offset,
};
try {
const result = await sdk.trigger({ function_id: "mem::graph-query", payload: req.body || {} });
const result = await sdk.trigger({ function_id: "mem::graph-query", payload });
return { status_code: 200, body: result };
} catch {
return graphDisabledResponse();
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,18 @@ export interface GraphQueryResult {
nodes: GraphNode[];
edges: GraphEdge[];
depth: number;
// #753: pagination + truncation signals for large graphs. `total*`
// counts reflect the full unbounded result for the given filter so
// the viewer can show "showing N of M" without re-querying. `truncated`
// is true when the default cap kicked in (operator may have wanted
// the full set but didn't ask for one).
totalNodes?: number;
totalEdges?: number;
truncated?: boolean;
// Echoes back the cap that produced this page so a paged client can
// detect when the default was applied vs an explicit `limit`.
limit?: number;
offset?: number;
}

export type ConsolidationTier =
Expand Down
67 changes: 58 additions & 9 deletions src/viewer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ <h1>agentmemory</h1>
var state = {
activeTab: 'dashboard',
dashboard: { loaded: false, health: null, sessions: [], memories: [], graphStats: null, recentAudit: [], lessons: [], crystals: [] },
graph: { loaded: false, nodes: [], edges: [], stats: null, filters: {}, selectedNode: null },
graph: { loaded: false, nodes: [], edges: [], stats: null, filters: {}, selectedNode: null, queryError: null, truncated: false, totalNodes: 0, totalEdges: 0 },
memories: { loaded: false, items: [], search: '', typeFilter: '' },
timeline: { loaded: false, observations: [], sessionId: '', minImportance: 0, page: 0, pageSize: 50 },
sessions: { loaded: false, items: [], selectedId: null },
Expand Down Expand Up @@ -1668,31 +1668,61 @@ <h1>agentmemory</h1>
}
}

// #753: previously POST'd `{}` and let the engine return the full
// graph. On 10k+ nodes the payload exceeded the iii state channel
// limit and the engine returned HTTP 500, leaving the tab blank
// with no signal. Now we pass an explicit `limit`, distinguish
// server error from empty-corpus, and render an actionable banner
// when the request itself fails instead of falling silent.
var GRAPH_INITIAL_LIMIT = 500;

async function loadGraph() {
var el = document.getElementById('view-graph');
el.innerHTML = '<div class="graph-container"><div class="graph-canvas-wrap"><canvas id="graph-canvas"></canvas><div class="graph-controls"><button title="Zoom In" data-action="zoom-graph" data-dir="1">+</button><button title="Zoom Out" data-action="zoom-graph" data-dir="-1">&minus;</button><div class="ctrl-divider"></div><button title="Recenter" data-action="recenter-graph">⌖</button></div><div class="graph-tooltip" id="graph-tooltip"></div></div><div class="graph-sidebar" id="graph-sidebar"></div></div>';

var results = await Promise.all([
apiPost('graph/query', {}),
apiPost('graph/query', { limit: GRAPH_INITIAL_LIMIT }),
apiGet('graph/stats')
]);
var queryResult = results[0] || { nodes: [], edges: [] };
var queryResult = results[0];
if (queryResult === null) {
// api() returns null only on non-2xx or a transport error; an
// empty graph would come back as { nodes: [], edges: [] }.
// Surface as an error state so #753-class failures don't read
// as "no data yet" — and so "Rebuild Graph" stays available.
state.graph.queryError = 'graph/query failed (check server logs for HTTP error)';
state.graph.nodes = [];
state.graph.edges = [];
state.graph.stats = results[1] || {};
state.graph.loaded = true;
renderGraphSidebar();
return;
}
state.graph.queryError = null;
state.graph.nodes = queryResult.nodes || [];
state.graph.edges = queryResult.edges || [];
state.graph.truncated = queryResult.truncated === true;
state.graph.totalNodes = typeof queryResult.totalNodes === 'number' ? queryResult.totalNodes : state.graph.nodes.length;
state.graph.totalEdges = typeof queryResult.totalEdges === 'number' ? queryResult.totalEdges : state.graph.edges.length;
state.graph.stats = results[1] || {};

if (state.graph.nodes.length === 0) {
if (state.graph.nodes.length === 0 && (state.graph.stats.totalNodes || state.graph.stats.nodes || 0) === 0) {
var sb = document.getElementById('graph-sidebar');
if (sb) sb.innerHTML = '<h3>Graph</h3><p style="font-size:12px;color:var(--ink-faint);margin:8px 0;font-style:italic;">No graph data yet. Building from observations and memories...</p>';
var buildResult = await apiPost('graph/build', {});
if (buildResult && buildResult.success && buildResult.nodes > 0) {
var freshResults = await Promise.all([
apiPost('graph/query', {}),
apiPost('graph/query', { limit: GRAPH_INITIAL_LIMIT }),
apiGet('graph/stats')
]);
var freshQuery = freshResults[0] || { nodes: [], edges: [] };
state.graph.nodes = freshQuery.nodes || [];
state.graph.edges = freshQuery.edges || [];
var freshQuery = freshResults[0];
if (freshQuery) {
state.graph.nodes = freshQuery.nodes || [];
state.graph.edges = freshQuery.edges || [];
state.graph.truncated = freshQuery.truncated === true;
state.graph.totalNodes = typeof freshQuery.totalNodes === 'number' ? freshQuery.totalNodes : state.graph.nodes.length;
state.graph.totalEdges = typeof freshQuery.totalEdges === 'number' ? freshQuery.totalEdges : state.graph.edges.length;
}
state.graph.stats = freshResults[1] || {};
Comment on lines +1718 to 1726

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Handle post-rebuild query failure.

If the rebuild succeeds but the subsequent graph/query request fails (returns null), the viewer does not set state.graph.queryError, so no error banner is shown. The user sees an empty graph with no indication that the query failed.

While rare (requires first load + successful build + failed query), this violates the PR objective to surface backend errors as a visible error state instead of falling silent.

🛡️ Proposed fix to set queryError when fresh query fails
       var freshResults = await Promise.all([
         apiPost('graph/query', { limit: GRAPH_INITIAL_LIMIT }),
         apiGet('graph/stats')
       ]);
       var freshQuery = freshResults[0];
       if (freshQuery) {
         state.graph.nodes = freshQuery.nodes || [];
         state.graph.edges = freshQuery.edges || [];
         state.graph.truncated = freshQuery.truncated === true;
         state.graph.totalNodes = typeof freshQuery.totalNodes === 'number' ? freshQuery.totalNodes : state.graph.nodes.length;
         state.graph.totalEdges = typeof freshQuery.totalEdges === 'number' ? freshQuery.totalEdges : state.graph.edges.length;
+      } else {
+        state.graph.queryError = 'graph/query failed after rebuild (check server logs)';
       }
       state.graph.stats = freshResults[1] || {};
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var freshQuery = freshResults[0];
if (freshQuery) {
state.graph.nodes = freshQuery.nodes || [];
state.graph.edges = freshQuery.edges || [];
state.graph.truncated = freshQuery.truncated === true;
state.graph.totalNodes = typeof freshQuery.totalNodes === 'number' ? freshQuery.totalNodes : state.graph.nodes.length;
state.graph.totalEdges = typeof freshQuery.totalEdges === 'number' ? freshQuery.totalEdges : state.graph.edges.length;
}
state.graph.stats = freshResults[1] || {};
var freshQuery = freshResults[0];
if (freshQuery) {
state.graph.nodes = freshQuery.nodes || [];
state.graph.edges = freshQuery.edges || [];
state.graph.truncated = freshQuery.truncated === true;
state.graph.totalNodes = typeof freshQuery.totalNodes === 'number' ? freshQuery.totalNodes : state.graph.nodes.length;
state.graph.totalEdges = typeof freshQuery.totalEdges === 'number' ? freshQuery.totalEdges : state.graph.edges.length;
} else {
state.graph.queryError = 'graph/query failed after rebuild (check server logs)';
}
state.graph.stats = freshResults[1] || {};
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/viewer/index.html` around lines 1718 - 1726, When handling the
post-rebuild freshResults, ensure we set a visible query error if the subsequent
graph/query failed: check freshResults[0] (freshQuery) and if it is falsy,
assign state.graph.queryError to freshResults[1] (or a small default error
object/message) so the UI shows an error banner; otherwise proceed to clear/set
state.graph.nodes, edges, truncated, totalNodes, totalEdges and ensure
state.graph.queryError is cleared when the query succeeds. Reference symbols:
freshResults, freshQuery, state.graph, state.graph.queryError, graph/query.

}
}
Expand All @@ -1719,7 +1749,26 @@ <h1>agentmemory</h1>
var nodeCount = gs.totalNodes !== undefined ? gs.totalNodes : (gs.nodes !== undefined ? gs.nodes : (gs.nodeCount || state.graph.nodes.length));
var edgeCount = gs.totalEdges !== undefined ? gs.totalEdges : (gs.edges !== undefined ? gs.edges : (gs.edgeCount || state.graph.edges.length));

var html = '<input type="text" class="graph-search" id="graph-search" placeholder="Search nodes...">';
var html = '';

// #753: error banner stays above the search box so a failed
// graph/query doesn't read as "0 nodes".
if (state.graph.queryError) {
html += '<div style="margin:8px 0;padding:10px 12px;border:1px solid var(--accent);background:var(--bg-alt);font-size:12px;color:var(--ink);line-height:1.4;">';
html += '<div style="font-weight:600;margin-bottom:4px;">Graph query failed</div>';
html += '<div style="font-size:11px;color:var(--ink-muted);">' + esc(state.graph.queryError) + '</div>';
html += '<button class="btn" data-action="rebuild-graph" style="margin-top:8px;font-size:11px;">Retry</button>';
html += '</div>';
} else if (state.graph.truncated) {
// #753: surface the cap so operators know they're not seeing
// the whole graph. Densest-first ranking on the server makes
// this the meaningful subset, not an arbitrary head.
html += '<div style="margin:8px 0;padding:10px 12px;border:1px solid var(--border);background:var(--bg-alt);font-size:11px;color:var(--ink-muted);line-height:1.4;">';
html += 'Showing ' + state.graph.nodes.length + ' of ' + state.graph.totalNodes + ' nodes (most-connected first). The full graph is too large to render at once.';
html += '</div>';
}

html += '<input type="text" class="graph-search" id="graph-search" placeholder="Search nodes...">';

html += '<h3 style="margin-top:16px;font-size:10px;text-transform:uppercase;letter-spacing:0.12em;color:var(--ink-muted);font-family:var(--font-ui);font-weight:700;">Graph Stats</h3>';
html += '<div style="display:flex;gap:20px;margin:10px 0 16px;padding:12px;background:var(--bg-alt);border:1px solid var(--border-light);border-radius:4px;">';
Expand Down
Loading