Skip to content

Search

This page explains how the results of search_nodes and search_memory_facts are produced: what Spongram does before the query reaches Graphiti, the stages it applies to Graphiti's answer, the optional result cache, and the counters it adds to a response. It also covers how get_episodes returns episodes in chronological order.

Edition terms used below: a server is any deployment with SPONGRAM_EDITION=cloud (the default), self-hosted included; the desktop edition is SPONGRAM_EDITION=local. For the arguments of each tool, see the tools reference.

Overview

Graphiti performs the retrieval itself. Spongram wraps the call:

  1. Before the call. It authenticates the brain key and injects the brain's group_id. When read scoping is on, it reads the scope argument and removes it from the request (Graphiti does not know it), and resolves project aliases. It looks the query up in the result cache when the cache is enabled. When PageRank is enabled, it multiplies max_nodes / max_facts so that the later stages have more candidates.
  2. The call. Graphiti answers with a list of nodes or facts.
  3. After the call. Spongram runs the stages below on that list, in a fixed order, then returns it.

Every stage is best-effort: if a stage fails, the error is logged and the list continues to the next stage unchanged. A ranking problem never turns a successful search into an error.

Stages applied to a result

The order below is the order of execution.

# Stage Applies to Controlled by
1 Read scoping search_nodes, search_memory_facts SPONGRAM_READ_SCOPING_ENABLED
2 Hide invalidated facts search_memory_facts SPONGRAM_HIDE_INVALID_FACTS
3 PageRank rerank search_nodes, search_memory_facts SPONGRAM_PAGERANK_ENABLED
4 Truncation to the requested size both always
5 Lexical rerank both SPONGRAM_RERANK_LEXICAL_ENABLED
6 Persona re-hoist both read scoping, with an active persona
7 Final truncation both always
8 Corroboration ranking search_memory_facts SPONGRAM_CORROBORATION_RANKING_ENABLED
9 Code-map enrichment search_nodes always
10 structuredContent synchronisation both always
11 Cache store both SPONGRAM_HOT_CACHE_ENABLED

Oversampling

When PageRank is enabled, Spongram asks Graphiti for more results than the client requested: max_nodes or max_facts multiplied by SPONGRAM_PAGERANK_OVERSAMPLE_FACTOR (default 3). The extra candidates give the reranking stages room to work. The client never receives them: stage 7 always cuts the list back to the size it asked for. Without PageRank the request is forwarded with the client's own value.

1. Read scoping

Memories are tagged at write time (project=, persona=, see Tagging and scope), but a search result does not carry the tags of the episodes it comes from. This stage looks them up in one graph query: for a fact, the episodes listed in its episodes field; for a node, the episodes that mention it. Each source episode is then judged:

Tags of the source episode Decision
persona= equal to the brain's active persona Kept, and placed at the head of the list
persona= of another persona Dropped, unless scope.all is true
No project= tag, or project=global Kept
project= equal to scope.project, or one of its aliases Kept
Any project=, when the call has no scope.project or has scope.all: true Kept
Any other project= Dropped

A result is kept as soon as one of its source episodes is kept. A result whose tags cannot be resolved is kept, and if the tag lookup itself fails the list is left untouched: the stage never removes something it could not judge. The number of results it removed is reported in _spongram.filtered.

The scope argument is an object: {"project": "<slug>"} keeps that project, global, untagged memories and the active persona, and {"all": true} disables the restriction. A project value that is not a valid slug (lower-case letters, digits, ., _, -, 64 characters at most) is ignored.

{
  "name": "search_memory_facts",
  "arguments": {
    "query": "deployment decisions",
    "max_facts": 10,
    "scope": {"project": "billing-api"}
  }
}

With the flag off, the scope argument is not offered in tools/list and results are not filtered. See Personas for the persona side of this stage.

2. Hide invalidated facts

Graphiti keeps a fact when newer information contradicts it and marks it instead. Spongram removes such facts from search_memory_facts results:

  • a fact with any expired_at value is removed;
  • a fact whose invalid_at is a date in the past is removed;
  • a fact with a missing, empty or unreadable invalid_at is kept.

The number removed is reported in _spongram.hidden_invalid. The flag is on by default; only an explicit 0, false, no or off turns it off, in which case superseded facts are returned as Graphiti ranks them. The facts stay in the graph either way.

3. PageRank rerank

This stage takes the results returned by Graphiti as seeds, looks them up as entities, loads the subgraph within two hops of them (at most SPONGRAM_PAGERANK_SUBGRAPH_LIMIT nodes, default 1000), runs personalised PageRank on it, and merges three rankings by reciprocal rank fusion: Graphiti's own order, the PageRank order, and the entity importance computed by the Sleeper when it exists. With SPONGRAM_PAGERANK_SURFACE_PPR_NODES (default on), entities that Graphiti did not return but that are strongly connected to the seeds can enter the list.

It falls back to Graphiti's order, without error, when there are fewer than three results, when the subgraph query returns nothing or exceeds SPONGRAM_PAGERANK_SUBGRAPH_TIMEOUT_MS (default 1500), or on any exception.

The code default is off. The desktop edition turns it on unless SPONGRAM_PAGERANK_ENABLED is explicitly false. On a server the subgraph query uses the APOC procedure apoc.path.subgraphAll, so APOC must be installed in Neo4j. Outcomes are counted in spongram_pagerank_rerank_total and spongram_pagerank_fallback_total.

4 and 7. Truncation

The list is cut to the max_nodes / max_facts the client sent. It happens twice so that both of these hold:

  • the lexical rerank (stage 5) only reorders the results that survived the cut, and cannot pull a low-ranked candidate of the oversampled list into the answer;
  • results of the active persona are never cut. When the list contains such results, the first cut is skipped and the only cut is stage 7, after the re-hoist.

Stage 7 runs on every path, including when PageRank fell back or is disabled, so an oversampled list never reaches the client.

5. Lexical rerank

A dependency-free reranker that scores each result by the fraction of query terms (three characters or more) found in its name, summary, fact and content fields, then sorts by that score. Ties keep their previous order. Off by default.

6. Persona re-hoist

Results kept in stage 1 because they belong to the active persona are moved back to the head of the list, in their current relative order, after the reranking stages may have moved them.

8. Corroboration ranking

The Dream worker gives each fact a confidence that grows with the number of distinct projects whose episodes support it. This stage lets that score adjust the final order of search_memory_facts, within narrow limits:

  • a fact may move up one place per additional project, three places at most;
  • it only passes a fact that is strictly less corroborated and not more relevant to the query (same lexical score as stage 5);
  • no fact is overtaken more than once, and a result of the active persona is never passed;
  • without a query, nothing moves.

Because it runs after the final truncation, it changes the order of the returned facts and never which facts are returned. The number of facts that moved up is reported in _spongram.promoted. A confidence is used only when the fact also carries confidence_episodes, which the Dream worker writes with it; until the worker has processed a brain, this stage does nothing there.

The code default is off; the server image and the desktop app enable it.

9. Code-map enrichment

For search_nodes, each returned entity that is linked to nodes of the code map gets an additional linked_code_nodes field. The stage only adds that field, and leaves the answer unchanged on any error.

10. structuredContent synchronisation

Graphiti returns each result twice: as a JSON string in result.content[0].text and as parsed JSON in result.structuredContent. Some clients read the first, others (Claude Code among them) the second. All the stages above work on content[0].text; this last stage copies the finished list into structuredContent, so both fields always carry the same results, the same order and the same _spongram block.

The _spongram block

When a stage has something to report, the result object carries a _spongram key next to nodes or facts:

Field Set by Meaning
filtered Read scoping Number of results removed because of their project or persona tags. Present, possibly 0, whenever the stage ran.
scope Read scoping The scope that was applied: project, all, persona (the active persona) and, when the project has aliases, aliases.
hidden_invalid Hide invalidated facts Number of superseded facts removed. Only present when at least one was.
promoted Corroboration ranking Number of facts that moved up. Only present when at least one did.
{
  "facts": [],
  "_spongram": {
    "filtered": 2,
    "scope": {"project": "billing-api", "all": false, "persona": null},
    "hidden_invalid": 1
  }
}

An agent that gets fewer results than expected can read these counters and, if filtered is not zero, repeat the search with {"scope": {"all": true}}.

Result cache

Spongram can keep recent search answers in memory and serve a repeated query without calling Graphiti. The cache is off by default (SPONGRAM_HOT_CACHE_ENABLED).

Property Value
Tools search_nodes, search_memory_facts
Lifetime of an entry SPONGRAM_HOT_CACHE_TTL_SEC, default 300 seconds
Capacity SPONGRAM_HOT_CACHE_MAX_SIZE, default 256 entries per process, least recently used evicted first
Stored value The fully processed answer, after all the stages above

What the key covers. The brain, the tool, the query text, the max_nodes / max_facts value the client sent, and every other argument that is forwarded to Graphiti (entity_types, edge_types, date filters, center_node_uuid, and so on). When read scoping is on, the key also carries the scope, the project aliases and the active persona; for search_memory_facts it carries the state of SPONGRAM_HIDE_INVALID_FACTS and of SPONGRAM_CORROBORATION_RANKING_ENABLED. Two calls share an entry only if they would have produced the same answer. A call without a query is never cached.

What is cached. Only an answer that is recognisably a success: a tool result that is not flagged as an error and that contains the tool's list (nodes or facts, possibly empty). Graphiti reports some failures as a normal answer containing an error text; these are not stored, so a transient failure is not replayed for the lifetime of an entry.

What invalidates it. clear_graph, delete_episode and delete_entity_edge drop every entry of the brain, including when the call ended with an unknown outcome. add_memory does not: extraction is asynchronous, so the graph has not changed when the call returns. A new memory therefore becomes visible to a cached query at the latest one lifetime after its extraction completes. Writes that do not go through the MCP endpoint (background workers, brain import, code-map ingest, admin routes) rely on the lifetime alone.

Warning

The cache lives in the memory of one Spongram process. It assumes a single replica of the spongram service; with several replicas, a delete handled by one would not clear the entries held by the others.

Chronological get_episodes

Graphiti returns the episodes of get_episodes ordered by identifier, which is not the order in which they were written. On a server Spongram answers this tool itself, from the graph, most recent first (created_at descending; episodes without a date come last). The answer has the same shape as Graphiti's, in both content[0].text and structuredContent.

  • The number of episodes comes from max_episodes, or last_n if max_episodes is absent; default 10, bounded to 1-500.
  • If the graph cannot be read, the call is forwarded to Graphiti as before.
  • The desktop edition's embedded engine already returns episodes chronologically.

SPONGRAM_EPISODES_CHRONO_ENABLED is on by default; only an explicit false value restores Graphiti's ordering.

"Server image" is the default of the published spongram image, which sets some flags that are off in the code. A variable set on the service overrides both. See Feature flags for how to change them.

Variable Code default Server image Effect
SPONGRAM_READ_SCOPING_ENABLED off on Filters results by project and persona tags; adds the scope argument. Also enabled by the desktop app.
SPONGRAM_PROJECT_ALIASES_ENABLED off on Lets a scoped search include a project's aliases. No effect on search without read scoping. Also enabled by the desktop app.
SPONGRAM_HIDE_INVALID_FACTS on on Removes superseded facts from search_memory_facts.
SPONGRAM_CORROBORATION_RANKING_ENABLED off on Lets facts confirmed by several projects move up. Also enabled by the desktop app.
SPONGRAM_PAGERANK_ENABLED off off Oversampling and PageRank rerank. On by default in the desktop edition.
SPONGRAM_PAGERANK_OVERSAMPLE_FACTOR 3 3 Multiplier applied to max_nodes / max_facts when PageRank is on.
SPONGRAM_RERANK_LEXICAL_ENABLED off off Lexical rerank.
SPONGRAM_HOT_CACHE_ENABLED off off Result cache.
SPONGRAM_HOT_CACHE_TTL_SEC 300 300 Lifetime of a cache entry.
SPONGRAM_HOT_CACHE_MAX_SIZE 256 256 Maximum number of cache entries.
SPONGRAM_EPISODES_CHRONO_ENABLED on on Chronological get_episodes on a server.

SPONGRAM_HIDE_INVALID_FACTS, SPONGRAM_READ_SCOPING_ENABLED, SPONGRAM_CORROBORATION_RANKING_ENABLED and SPONGRAM_EPISODES_CHRONO_ENABLED are read on each call. The PageRank, lexical rerank and cache variables are read once when the process starts.