Cortex and Wiki¶
Spongram serves two read-oriented views of a brain from its own HTTP port: the Cortex, a 3D explorer of the memory graph, and the Wiki, one Markdown page per entity that can be exported to an Obsidian vault. This page covers their URLs, how they authenticate, the live stream that feeds the Cortex, the operator mirror in the admin UI, and the way the Wiki renders brain content safely. The third page built on the same engine, the code map explorer, is described in Code map.
Pages at a glance¶
| Page | URL | Shows |
|---|---|---|
| Cortex | GET /v1/graph |
Entities and facts of the brain as a 3D force-directed graph |
| Wiki | GET /v1/wiki |
One Markdown page per entity: summary, relations, source episodes |
| Code map | GET /v1/codemap |
The structural graph of ingested repositories |
All three are served by the spongram service on a server and in the desktop edition. Each
page is a single HTML file rendered by the server, with no build step of its own; they share one
pre-built JavaScript bundle (see Rendering engine).
Every query a page makes is scoped on the server to the group_id of the authenticated brain. A client
cannot ask for another brain's graph.
Authentication¶
With a brain key¶
On a server the HTML page itself is public and contains no data. The page obtains the brain
key in the browser and sends it as Authorization: Bearer spt_brain_xxx on every data request. It looks
for the key in this order:
- the URL fragment, for example
https://spongram.example.com/v1/graph#key=spt_brain_xxx. A fragment is never sent to the server, so the key does not appear in access logs. The page removes it from the address bar as soon as it has read it; - the browser's session storage for the tab, where the page keeps the key after a first successful entry;
- a form asking for the key.
Once the Cortex has identified the brain, it also stores the key in the browser's local storage for that origin, so that the admin UI can reopen the explorer without asking again. Treat a browser profile that has opened a Cortex as holding that brain key.
On a server each data request counts against the brain's rate limit
(SPONGRAM_RATE_LIMIT_PER_MIN, 100 by default; 0 disables it). A 429 carries a Retry-After
header.
Desktop edition¶
The desktop edition is single-user and has no brain keys: the pages skip the key form and the data routes answer without a key. The server tells the page which edition it runs in; that value only decides whether the form is shown. Access control is always enforced by the data routes.
Operator mirror¶
An operator can open the Cortex, the Wiki and the code map of any brain without knowing its brain key. The admin UI embeds them from the instance detail page (see Admin UI), using mirror routes under the admin API:
| Mirror | Page | Data |
|---|---|---|
| Cortex | /admin/api/spongram/instances/{id}/cortex |
.../cortex/data/*, .../cortex/stream |
| Wiki | /admin/api/spongram/instances/{id}/wiki |
.../wiki/index.json, .../wiki/themes.json, .../wiki/export.zip, .../wiki/{slug} |
| Code map | /admin/api/spongram/instances/{id}/codemap |
.../codemap/data/* |
The mirror serves the same HTML and delegates every data route to the same handler as the /v1 route.
Two things differ:
- the page reads the admin token from the fragment
#admin=<admin-token>instead of#key=, keeps it in session storage only, and sends it as theAuthorizationheader of each data request; - the brain is selected by the instance id in the path, and its
group_idis looked up on the server.
The page shell of a mirror is reachable without a header, because a browser navigation cannot carry one;
it returns 404 for an unknown instance and contains no data. Every data, stream and delete route of the
mirror requires the admin token.
Warning
The admin token in a mirror page, and the console session in the Cloud control plane, live in the same browser origin as the pages. This is why brain content is never inserted into these pages as raw HTML (see Safe Markdown rendering).
With the Cloud control plane enabled, the mirrors authenticate with the console session instead of a token in the fragment, each request is checked against the user's permissions on the brain, and changes send a CSRF token.
The Cortex¶
The Cortex shows the memory graph: entities, the communities they are grouped into, and the facts between them. Episodes, the entity summaries written by the Sleeper and code map nodes are not drawn. The side panel offers:
- a project list, built from the
project=tags of episodes, to restrict the graph to the entities mentioned by one project's episodes; - entity type filters;
- a search over entity names and summaries;
- the most recent episodes;
- for a selected node: its neighbours and facts, the summary compiled by the Sleeper when one exists (see Sleeper and Dream), and a "forget" action.
When personas are enabled, the header links to the Personas page.
Data routes¶
All routes below require the brain key (or, under the mirror prefix, the admin token).
| Route | Returns |
|---|---|
GET /v1/graph/data/stats |
Brain identity and counts of nodes, edges and episodes |
GET /v1/graph/data/full |
A page of entity nodes and their links. Query: limit (1 to 2000, default 500), offset, label, project, raw_links (1 returns links whose other end is outside the page). The answer carries page.has_more |
GET /v1/graph/data/search?q=&limit= |
Entities whose name or summary contains q (limit 1 to 200, default 20) |
GET /v1/graph/data/node/{uuid} |
One node and up to 50 neighbours; 404 outside the brain |
GET /v1/graph/data/episodes |
Recent episodes, newest first. Query: limit (1 to 500, default 50), project, persona |
GET /v1/graph/data/labels?project= |
Entity types with counts |
GET /v1/graph/data/projects |
Project tags with episode counts |
GET /v1/graph/data/entity/{uuid}/summary |
The Sleeper summary of an entity, or {"available": false} |
DELETE /v1/graph/data/entity/{uuid} |
Forgets an entity (see below) |
GET /v1/graph/stream |
Live updates (see below) |
The page loads the graph in pages of 2000 nodes until the server reports no further page, up to 80 000 nodes, and resolves links across pages itself.
With project aliases enabled, episodes?project= also returns the episodes tagged with an alias of that
project (see Project aliases).
Forgetting an entity¶
DELETE /v1/graph/data/entity/{uuid} permanently deletes one entity, its relations and its Sleeper
summary. The episodes that mentioned it are kept, so the source material survives. The route answers
404 when the entity is not in the authenticated brain. The holder of a brain key can forget anything in
that brain; with the Cloud control plane enabled, the action additionally requires the memory.delete
permission.
Live stream¶
GET /v1/graph/stream is a Server-Sent Events stream that keeps an open Cortex up to date.
Warning
Read this stream with fetch() and a ReadableStream reader, never with EventSource.
EventSource cannot send an Authorization header, so using it would force the brain key into the
query string, where it ends up in logs, proxies and browser history.
const response = await fetch("/v1/graph/stream", {
headers: { Authorization: "Bearer " + key },
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
for (;;) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const frames = buffer.split("\n\n");
buffer = frames.pop();
for (const frame of frames) handle(frame); // "event: <name>\ndata: <json>"
}
The server checks the graph every 5 seconds and emits:
| Event | Payload |
|---|---|
ready |
Sent once on connection, with the timestamp the stream starts from |
nodes |
Entities created since the last check (at most 50 per check) |
episodes |
Episodes created since the last check (at most 50 per check) |
ping |
Sent when a check found nothing new, to keep the connection open |
activity |
A search has just been served on this brain (only with the activity focus enabled) |
error |
A check failed; the server then closes the stream |
The response sets Cache-Control: no-cache and X-Accel-Buffering: no. If you put a reverse proxy in
front of Spongram, make sure it does not buffer this route. The page reconnects by itself 5 seconds after
a disconnection.
Activity focus¶
With the activity focus enabled, an open Cortex shows what the brain is doing for its agents: when a search is answered, the nodes that were returned light up while the rest of the graph dims, then the view returns to normal.
How it works:
- After each successful
search_nodesorsearch_memory_factscall, whether it was computed or served from the cache, Spongram publishes an event for that brain. The event holds only identifiers: the uuids of the returned nodes or, for facts, of their two end nodes and the pairs they connect, capped at - It never contains the query text or any content.
- The stream forwards it immediately as an
activityevent, without waiting for the next 5-second check. - Writes do not publish events. Instead, each item of the
episodesevent carries anentity_uuidslist (the entities that episode mentions, at most 50), which the page uses to light up what a new memory touched. - In the page, the focus fades in over half a second, holds for 6 seconds and fades out. The camera frames the lit area only if the user has not moved the view in the last 10 seconds, and returns to the previous view afterwards. A click, a drag or a scroll on the view hands control back at once.
| Setting | Default | Effect |
|---|---|---|
SPONGRAM_CORTEX_ACTIVITY_ENABLED |
off in the code; the server image and the desktop app turn it on | Enables the activity event and the entity_uuids field. When off, the stream is exactly the one described in the previous section |
To turn it off on a server, set SPONGRAM_CORTEX_ACTIVITY_ENABLED=false in the service environment; a
service-level value overrides the image default. See Feature flags.
Note
The activity bus is held in memory, per brain, inside the spongram process. It is correct as long
as the service runs as one replica with one process, which is how the provided stack files run
it. With several
replicas, a search served by one process would not reach a Cortex connected to another: the focus
would silently stop working for some viewers. Nothing else depends on this bus. During a rolling
update two containers overlap briefly and an event can be missed, which has no effect on memory.
An event is dropped rather than delayed when a viewer is slow (each connection buffers 16 events and discards the oldest), and publishing costs nothing when no Cortex is open on the brain.
Rendering engine¶
The three pages load one script, /admin/assets/cortex/bundle.js, which contains the 3D engine and the
Markdown parser. It is built once with the admin UI and shipped inside the server image and the desktop
app; a second file next to it, layout.worker.js, computes the graph layout in a web worker. Nothing is
fetched from a CDN or any other origin, so the explorers work on a network without Internet access.
On a server the bundle is served from the admin UI build directory (SPONGRAM_ADMIN_DIST_DIR,
/app/admin_dist by default). If that directory is missing, the pages load but can neither draw the
graph nor render Wiki pages.
The engine draws the whole brain in a handful of draw calls regardless of its size: one instanced mesh for all node cores, one point cloud for their halos, one line-segment object for all links, a bounded pool of travelling signals, and a bloom pass. Hover and click are resolved in screen space against node positions. This is what keeps a graph of tens of thousands of nodes interactive; one 3D object per node and per link does not scale to that size.
The Wiki¶
How pages are compiled¶
Wiki pages are compiled by the Sleeper. Each time it writes the summary of an entity, it renders a Markdown page for that entity and stores it in the SQL database, keyed by brain and entity. A page's version number increases only when its text changes.
Compilation is controlled per brain by the wiki_enabled setting, on by default; it can be changed
from the brain's page in the admin UI or through the admin API (see HTTP API).
When it is off, summaries are still written and existing pages stay readable, but pages are no longer
updated. If the Sleeper is disabled, no page is compiled at all.
A page contains:
| Part | Content |
|---|---|
| YAML front matter | title, slug, type, types, uuid, created, summary_updated, sources, projects, locale, generator |
| Summary | The Sleeper summary, or the short extraction summary when none exists yet |
| Relations | Outgoing and incoming facts (30 each at most), with the other entity written as an Obsidian link [[Entity name]] |
| Sources | The 50 most recent episodes mentioning the entity, each truncated to 400 characters, with its tags |
Section headings are written in the brain's language (French or English). The summary, the facts and the
episode text stay in the language they were written in. The slug is the entity name in lower case with
every run of characters outside a-z, 0-9, ., _ and - replaced by a hyphen.
Routes¶
| Route | Returns |
|---|---|
GET /v1/wiki |
The Wiki browser page |
GET /v1/wiki/index.json |
All pages of the brain: slug, title, version, updated_at, project and source counts |
GET /v1/wiki/{slug} |
One page as text/markdown; 404 for an unknown slug |
GET /v1/wiki/export.zip |
Every page as spongram/<slug>.md in one archive, to drop into an Obsidian vault |
GET /v1/wiki/themes.json?limit= |
Communities of related entities found by the Dream, with member previews and a summary when one exists (limit 1 to 100, default 20) |
The data routes require the brain key. The Wiki browser authenticates like the Cortex and shares its stored key, so moving between the two does not ask for the key again.
Safe Markdown rendering¶
A wiki page is untrusted input. It embeds episode text and tags, which anyone able to write to the brain
controls, together with entity names and model-written summaries. A Markdown parser on its own lets raw
HTML through and does not filter link targets, so a memory containing an image tag with an event handler,
or a link with a javascript: target, would execute in the page; in an operator mirror that page holds
the admin token.
The Wiki browser therefore renders Markdown through two independent layers:
- In the parser. Raw HTML, block or inline, is displayed as escaped text (HTML comments are
dropped). Link and image targets must be
http,https,mailto, relative or an in-page anchor; anything else, including scheme-relative targets that leave the origin, is reduced to its text. Titles and alternative texts are escaped again. - On the result. The generated HTML is parsed in an inert template element and filtered against an allow-list of the tags and attributes the renderer emits (headings, paragraphs, lists, emphasis, code, block quotes, tables, links, images, disabled checkboxes). Any other element is replaced by its text, any other attribute is removed, and link targets are checked a second time. The resulting nodes are attached to the page as DOM nodes, never as an HTML string.
Everywhere else in the explorers (names, summaries, facts, episode text, error messages), values are HTML-escaped before they are inserted.
Note
The same caution applies to what you build on these routes. GET /v1/wiki/{slug} and the data routes
return brain content verbatim; escape or sanitise it before displaying it in a browser.
Language¶
The pages are available in French and English. Add ?lang=en or ?lang=fr to the page URL; a missing
or unknown value falls back to French and never causes an error. The parameter applies to the interface
only. The language of Wiki section headings is the brain's own language setting, applied when the
Sleeper compiles the page.