Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

docs: explain breadth-first graph walks and their speedups

garrytan2be2875dabdc
diff --git a/README.md b/README.md
index 4c4dbe60b9..970bd0bb9f 100644
--- a/README.md
+++ b/README.md
@@ -26,7 +26,7 @@ I'm Garry Tan, President and CEO of Y Combinator. I built GBrain to run my own A
Alongside keyword and semantic retrieval, GBrain offers two optional ways to use your stored knowledge:
- **A synthesis layer that gives you the actual answer.** Synthesized, well-cited prose across people, companies, deals, and ideas. Not "here are 10 chunks that mention your query"; an actual answer with citations and an explicit note on what the brain doesn't know yet. The gap analysis is the part that changes how you use the brain.
-- **A typed knowledge graph.** Trusted local page writes extract supported references without LLM calls when auto-linking is enabled. Remote `put_page` does not extract graph edges inline: stdio has best-effort startup/idle sweeps; HTTP needs explicit maintenance or authorized `add_link` calls. Ask "who works at acme-example?" to use stored relationships. On BrainBench relationship questions a graph adapter scored **P@5 0.3421, R@5 0.9791** vs 0.1917 / 0.6874 plain hybrid ([2026-09-09 refresh](https://github.com/garrytan/gbrain-evals/blob/main/docs/benchmarks/2026-09-09-retrieval-refresh.md)): whole systems, not a graph-only lift or a general guarantee.
+- **A typed knowledge graph.** Trusted local page writes extract supported references without LLM calls when auto-linking is enabled. Remote `put_page` edges arrive later, via sweeps, maintenance or `add_link`. Ask "who works at acme-example?": walks are breadth-first, milliseconds on dense clusters ([how](docs/architecture/RETRIEVAL.md#breadth-first-graph-walks)). On BrainBench relationship questions a graph adapter scored **P@5 0.3421, R@5 0.9791** vs 0.1917 / 0.6874 plain hybrid ([2026-09-09 refresh](https://github.com/garrytan/gbrain-evals/blob/main/docs/benchmarks/2026-09-09-retrieval-refresh.md)): whole systems, not a graph-only lift or a general guarantee.
The point of building a 150K-page brain is to use it as a strategic moat. To never lose context. To query what's in your own head without re-reading it. The brain layer is what makes the moat usable. The 24/7 dream cycle is what keeps it sharp. Both run on your hardware, your DB, your keys.
diff --git a/docs/architecture/RETRIEVAL.md b/docs/architecture/RETRIEVAL.md
index 448c4998a0..06fbb230c5 100644
--- a/docs/architecture/RETRIEVAL.md
+++ b/docs/architecture/RETRIEVAL.md
@@ -393,6 +393,39 @@ pages at the top as the pin allows, where an unpinned run would place one at
`limit`. Turn it off per brain with `gbrain config set search.relational_rerank_pin off`.
The knob folds into the query-cache key (`rrp=`).
+### Breadth-first graph walks
+
+Since v0.60.293.0 every graph walk (`traverse_graph`, `gbrain graph`, the graph
+context `think` gathers) is breadth-first. Each page is visited once, at its
+shortest distance from the start page, and each edge comes back once, one hop
+past its shallower walked endpoint. The old walk followed every possible route,
+so a densely linked cluster multiplied the work with every hop (a 12-page
+clique at depth 5 took 22 s before a 20,000-path cap bounded it) and returned
+the same page and edge again at every depth some route reached it.
+
+What that changes for you:
+
+- **Faster on the graphs that were slow.** On Postgres, a depth-5 walk over a
+ 12-page clique dropped from 44–91 ms to 4–9 ms, and walks from a 400-spoke hub
+ got 1.8x to 4x faster (`scripts/bench-graph-walk.ts`, medians, against v0.60.292.0).
+- **Complete answers.** The old cap counted routes, so a busy neighbourhood
+ could stop the walk before it reached deeper pages. The new cap counts
+ visited pages (20,000, shallowest first), so a walk reaches every page within
+ its depth unless the brain itself is that large.
+- **Cleaner output.** No repeated pages or edges at deeper depths, so an
+ agent's context holds each relationship once and `gbrain graph` prints each
+ edge once.
+- **Same safety.** Soft delete, private pages and private link origins, source
+ scope, link type and temporal policy still apply at every hop: a page the
+ caller may not read never relays a walk (`test/traverse-bfs.test.ts`).
+
+The walk is one SQL statement on both engines: a recursive CTE whose rows are
+whole layers (`bfsWalkCtes` in `src/core/engine-sql/links.ts`). Walks that reach
+most of a large graph in both directions are still dominated by turning tens
+of thousands of edges into results before the 5,000-row cap, about half a
+second on a 10,000-page, 60,000-link graph; speeding that projection up is
+open work.
+
### Multi-hop relationship chains
A question that chains two or three relationships ("who founded the companies
diff --git a/docs/mcp/TOOL_REFERENCE.md b/docs/mcp/TOOL_REFERENCE.md
index 6fa28c1a6f..a069435c4a 100644
--- a/docs/mcp/TOOL_REFERENCE.md
+++ b/docs/mcp/TOOL_REFERENCE.md
@@ -444,12 +444,12 @@ Submit an LLM agent job that the worker dispatches via the gateway-native tool l
## `traverse_graph`
-Traverse link graph from a page. Remote callers default to bidirectional edges (GraphPath[]) at depth 2 (pass depth explicitly for deeper walks); trusted local no-filter callers get the legacy node shape at depth 5.
+Traverse link graph from a page. Remote callers default to bidirectional edges (GraphPath[]) at depth 2 (pass depth explicitly for deeper walks); trusted local no-filter callers get the legacy node shape at depth 5. The walk is breadth-first: each page comes back once at its shortest depth and each edge once, and a walk stops at 20,000 visited pages or 5,000 edge rows, keeping the shallowest (a stderr note says when).
| Parameter | Type | Guidance |
| --- | --- | --- |
| `slug` (required) | string | Slug of the page to start the traversal from, e.g. 'people/alice-example'. This is the start-node param — there is no `start` or `root` param. |
-| `depth` | number | Max traversal depth (capped at 10). Default 5, except a remote call that lets direction default to 'both' uses 2 (bidirectional path enumeration is combinatorial on hubs); an explicit depth is always honored up to the cap. |
+| `depth` | number | Max traversal depth (capped at 10). Default 5, except a remote call that lets direction default to 'both' uses 2 (a both-direction walk from a hub reaches much of the brain and fills the 5,000-edge cap); an explicit depth is always honored up to the cap. |
| `link_type` | string | Filter to one link type (per-edge filter, traversal only follows matching edges) |
| `direction` | string (`in`, `out`, `both`) | Traversal direction ('in', 'out', or 'both'). Remote callers default to 'both'; trusted local no-filter callers get the legacy outgoing-node output. |
| `hops` | array of `{link_type, toward}` | Typed chain of 1-3 hops from the start page, e.g. `[{"link_type":"invested_in","toward":"object"},{"link_type":"founded","toward":"subject"}]` (companies the person invested in, then their founders). `toward` follows the relationship's meaning (`object`: subject → object; `subject`: object → subject), not stored link direction. Returns `{anchor, answers, paths, diagnostics}` with each answer's evidence edges; a chain that finds nothing carries a `relational_chain` notice with the next call. Not combinable with `depth`, `link_type` or `direction`; each hop walks live relationships. Advertised on the full surface only. Chain link types: founded, invested_in, led_round, advises, works_at, attended, yc_partner. See docs/guides/multi-hop.md. |
diff --git a/llms-full.txt b/llms-full.txt
index a60b4a747f..9986a59b6b 100644
--- a/llms-full.txt
+++ b/llms-full.txt
@@ -4846,7 +4846,7 @@ I'm Garry Tan, President and CEO of Y Combinator. I built GBrain to run my own A
Alongside keyword and semantic retrieval, GBrain offers two optional ways to use your stored knowledge:
- **A synthesis layer that gives you the actual answer.** Synthesized, well-cited prose across people, companies, deals, and ideas. Not "here are 10 chunks that mention your query"; an actual answer with citations and an explicit note on what the brain doesn't know yet. The gap analysis is the part that changes how you use the brain.
-- **A typed knowledge graph.** Trusted local page writes extract supported references without LLM calls when auto-linking is enabled. Remote `put_page` does not extract graph edges inline: stdio has best-effort startup/idle sweeps; HTTP needs explicit maintenance or authorized `add_link` calls. Ask "who works at acme-example?" to use stored relationships. On BrainBench relationship questions a graph adapter scored **P@5 0.3421, R@5 0.9791** vs 0.1917 / 0.6874 plain hybrid ([2026-09-09 refresh](https://github.com/garrytan/gbrain-evals/blob/main/docs/benchmarks/2026-09-09-retrieval-refresh.md)): whole systems, not a graph-only lift or a general guarantee.
+- **A typed knowledge graph.** Trusted local page writes extract supported references without LLM calls when auto-linking is enabled. Remote `put_page` edges arrive later, via sweeps, maintenance or `add_link`. Ask "who works at acme-example?": walks are breadth-first, milliseconds on dense clusters ([how](docs/architecture/RETRIEVAL.md#breadth-first-graph-walks)). On BrainBench relationship questions a graph adapter scored **P@5 0.3421, R@5 0.9791** vs 0.1917 / 0.6874 plain hybrid ([2026-09-09 refresh](https://github.com/garrytan/gbrain-evals/blob/main/docs/benchmarks/2026-09-09-retrieval-refresh.md)): whole systems, not a graph-only lift or a general guarantee.
The point of building a 150K-page brain is to use it as a strategic moat. To never lose context. To query what's in your own head without re-reading it. The brain layer is what makes the moat usable. The 24/7 dream cycle is what keeps it sharp. Both run on your hardware, your DB, your keys.