Sleeper and Dream¶
Spongram runs two background workers inside the spongram process. The Sleeper runs every
couple of minutes and uses a language model to compile summaries. The Dream runs every few
hours and consolidates the graph without any model call. This page describes what each one
writes, how to tune them, and how their state appears in the health endpoints.
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.
At a glance¶
| Sleeper | Dream | |
|---|---|---|
| Purpose | Entity summaries, wiki pages, entity importance, persona summaries | Confidence, decay, duplicate detection, communities, re-queuing stale summaries |
| Uses the language model | Yes, role sleeper |
No |
| Default interval | 120 seconds | 6 hours |
| Enabled by | SPONGRAM_SLEEPER_ENABLED |
SPONGRAM_DREAM_ENABLED |
| Scope of one pass | All brains | All brains, one after the other |
Both switches are off in the code and on in the server image. Each worker is a single loop per
process, with in-memory state: run one replica of the spongram service.
Sleeper¶
What it compiles¶
For each entity, the Sleeper maintains:
- a
Summarynode, linked to the entity by aSUMMARIZESrelationship: a plain-text synthesis of at most two paragraphs (about 120 words), written in the language of the source episodes, from every episode that mentions the entity and from its neighbouring entities and facts; - the entity's
importance, an integer from 1 to 10 computed without the model from the number of episodes that mention it and the number of its relationships. The PageRank stage of search uses it; - a wiki page for the entity, stored in the relational database and shown by the
Wiki, unless the brain's
wiki_enabledsetting is off.
For each persona, when persona summaries are enabled, it also maintains the "what I already know" summary described in Personas.
Summary nodes are not entities, and the Cortex graph does not draw them.
One tick¶
- Circuit-breaker check. If the breaker of the language model is open, the tick is skipped (see below).
- Persona summaries. Up to
SPONGRAM_PERSONA_SUMMARY_MAX_PER_TICKpersonas (default5) are examined, most activated first, rotating through the list tick after tick. For each, the 40 most recent episodes taggedpersona=<slug>are read. If that set of episodes is the one the current summary was built from, nothing is done. Otherwise the model writes a summary of at most 120 words in the persona's language. - Scan. The Sleeper lists the entities whose most recent mentioning episode is newer than their summary, or that have no summary: entities without a summary first, then the most recently mentioned. The scan covers up to ten times the per-tick budget, with a minimum of 200 and a maximum of 500 entities.
- Clean-marking in batches. A summary records a version: a hash of the identifiers of the episodes it was built from. One grouped query computes the current version of every scanned entity. Entities whose episode set has not changed are marked up to date in a single write. They cost no model call and do not count against the budget.
- Synthesis. The remaining entities, at most the per-tick budget, are summarised by the
model,
SPONGRAM_SLEEPER_CONCURRENCYat a time (default4). Those beyond the budget wait for the next tick. - Grounding check. A new summary is written only if at least
SPONGRAM_SLEEPER_GROUNDING_MIN(default0.3) of its content words also appear in the source episodes. A summary below that ratio is discarded as a probable invention, and the entity is marked up to date until a new episode mentions it.
Failures are contained: if the grouped version check fails, every scanned entity is treated as changed (still within the budget); if the grouped write fails, those entities are examined again at the next tick; an error on one entity does not affect the others. When a whole tick raises, the Sleeper waits longer before the next one, up to eight times the interval.
Interval and budget¶
The interval and the per-tick budget are runtime settings stored in the database
(inference.sleeper_interval_sec and inference.sleeper_max_per_tick), seeded with 120 and
10. They are read at every tick, so a change made in the Settings page of the
admin UI applies without a restart. The environment variables
SPONGRAM_SLEEPER_INTERVAL_SEC and SPONGRAM_SLEEPER_MAX_PER_TICK are used only when the
database holds no value.
The budget bounds model calls only. Raise it to absorb a backlog faster, at the price of more load on the model server; the scan and the batch clean-marking adjust with it.
The model itself is the one configured for the sleeper role (see
Inference providers). Calls go through the internal
inference proxy, authenticated with SPONGRAM_INTERNAL_AUTH_TOKEN: without that variable the
Sleeper logs a warning and does not start.
Circuit breaker and the throttled state¶
The inference proxy keeps a circuit breaker for chat completions. After
SPONGRAM_BREAKER_FAILURE_THRESHOLD consecutive upstream failures (default 5; server errors
and transport errors count, client errors do not) it opens and refuses calls for
SPONGRAM_BREAKER_COOLDOWN_SEC (default 60). It then lets one probe request through: success
closes it, failure doubles the cool-down, up to SPONGRAM_BREAKER_MAX_COOLDOWN_SEC (default
600).
The Sleeper looks at the breaker before each tick. While it is open, the tick is skipped: no
graph scan, no model call. A Sleeper in that situation is reported as throttled, which is a
healthy state: it is waiting for the model server, not broken. If the skipping stops and no
tick follows, the state becomes stalled.
Breaker activity is exported as spongram_breaker_state, spongram_breaker_opens_total,
spongram_breaker_closes_total and spongram_breaker_probes_total; Sleeper activity as
spongram_sleeper_summaries_total, spongram_sleeper_skipped_unchanged_total,
spongram_sleeper_skipped_empty_total, spongram_sleeper_errors_total,
spongram_sleeper_last_tick_unixtime and spongram_sleeper_last_tick_duration_seconds.
Dream¶
At each pass the Dream lists the brains that hold entities and runs the following steps on each of them, in this order. Most steps are protected individually (a failure is logged and the step skipped), and an error that ends the pass for one brain does not stop the others. No step deletes or merges anything.
1. Probable duplicates¶
Entities that probably denote the same thing under different names are detected in two stages:
- By name, in memory. Names are normalised (case, accents, punctuation, spaces) and cut
into character trigrams. Two entities are candidates when they share a type, their normalised
names differ, and the Jaccard similarity of their trigrams reaches
SPONGRAM_DREAM_DUP_NAME_SIMILARITY(default0.5). At most 2 000 candidate pairs are kept. - By embedding, in the graph. Only for those pairs, the cosine similarity of the name
embeddings must reach
SPONGRAM_DREAM_DUP_EMBEDDING_FLOOR(default0.40, raw cosine in both editions).
The 100 best pairs per brain are counted, and the first ten are written to the log
(dream duplicate candidate ...). They are reported for review only. Entities with exactly the
same normalised name are the business of step 6. The former SPONGRAM_DREAM_DUP_THRESHOLD is
ignored.
2. Refreshing neighbours¶
When an entity has at least SPONGRAM_DREAM_BACKPROP_MIN_EDGES relationships (default 3), the
latest of which was created within SPONGRAM_DREAM_BACKPROP_WINDOW_HOURS (default 24), the
neighbours it is linked to by a relationship created in that window have their summary marked
stale. The Sleeper rewrites them at its next ticks, so that a summary reflects what was learnt
about related entities.
3. Confidence¶
Each fact gets a confidence that is a function of its evidence: the number of distinct
projects (project= tags) among the episodes that support it.
| Distinct projects supporting the fact | confidence |
|---|---|
| 1 (or no project tag) | 0.50 |
| 2 | 0.65 |
| 3 | 0.80 |
| 4 | 0.95 |
| 5 or more | 1.00 |
That is 0.50 + 0.15 per additional project, capped at 1.0 and never below 0.50. The value
is recomputed identically at every pass, so it cannot drift. Alongside it the Dream stores
confidence_episodes (the number of supporting episodes) and last_confidence_update. The
latter moves only when the evidence changes: an episode added or removed, or a different
computed value. A fact with no confidence, or with a value outside 0.50-1.00, is set to
0.50.
Search can use this score to let corroborated facts move up a few places.
4. Decay¶
For every fact the Dream computes
where age_days is the time since last_confidence_update and tau is
SPONGRAM_DREAM_DECAY_TAU_DAYS (default 30): the score halves about every 21 days without new
evidence. decayed_score is stored on the fact. Search does not read it.
5. Forgetting (off by default)¶
With SPONGRAM_DREAM_FORGETTING_ENABLED=true, a fact whose decayed_score falls below
SPONGRAM_DREAM_FORGET_FLOOR (default 0.05) gets decayed_score = 0 and a forgotten_at
date. The fact stays in the graph and search still returns it; the mark is cleared when the
fact's evidence changes.
It is off because elapsed time alone triggers it: a fact stated once has a confidence of 0.50
and crosses the floor after about 69 days, whether or not agents still read and rely on it.
Enable it only once you have decided what the mark should mean for your users.
Note
The older variable SPONGRAM_DREAM_FORGET_ENABLED is ignored. If it is set, the service
logs a warning at startup and forgetting stays off.
6. Same-name linking (off by default)¶
With SPONGRAM_DREAM_RESOLVE_ENABLED=true, entities of a brain whose names are equal once
lower-cased and trimmed are linked by a SAME_AS relationship. Nothing is merged. When two
entities are linked this way and one has a more recent summary, the other is handed back to the
Sleeper (this part always runs, and does nothing without SAME_AS links).
7. Communities¶
A label-propagation algorithm assigns each entity a community_id: every entity starts in its
own community and repeatedly adopts the most frequent community among its neighbours. Each
community is then given a deterministic label built from its most important members, stored as
a CommunitySummary node, and the labels are rolled up into one digest per brain.
Interval and restarts¶
The Dream runs every SPONGRAM_DREAM_INTERVAL_SEC (default 21600, six hours). The time of
the last successful pass is saved in the database (runtime setting worker.dream.last_tick_at).
When the service restarts, the Dream waits for the remainder of the interval instead of running
at once: several deployments in a day do not cause as many passes, each of which would send
hundreds of summaries back to the Sleeper. If the saved time is missing, unreadable or in the
future, the pass runs immediately.
After a failed pass the Dream waits longer before retrying, up to eight times the interval.
In the desktop edition the interval is SPONGRAM_LOCAL_DREAM_INTERVAL_SEC (default 600).
Environment variables¶
"Server image" is the value set by the published spongram image when it differs from the code
default. See Feature flags for how to override a value.
| Variable | Code default | Server image | Meaning |
|---|---|---|---|
SPONGRAM_SLEEPER_ENABLED |
off | on | Starts the Sleeper. Needs SPONGRAM_INTERNAL_AUTH_TOKEN. |
SPONGRAM_SLEEPER_INTERVAL_SEC |
120 |
120 |
Seconds between ticks, when the database holds no value. |
SPONGRAM_SLEEPER_MAX_PER_TICK |
10 |
10 |
Model syntheses per tick, when the database holds no value. |
SPONGRAM_SLEEPER_CONCURRENCY |
4 |
Syntheses run in parallel. | |
SPONGRAM_SLEEPER_GROUNDING_MIN |
0.3 |
Minimum share of a summary's words found in its sources. | |
SPONGRAM_PERSONA_SUMMARY_ENABLED |
off | on | Persona summaries. Requires SPONGRAM_PERSONAS_ENABLED. |
SPONGRAM_PERSONA_SUMMARY_MAX_PER_TICK |
5 |
Personas examined per tick. | |
SPONGRAM_THINKING_SLEEPER |
off |
Reasoning level of the model for the sleeper role: off, low, medium, xhigh. |
|
SPONGRAM_BREAKER_FAILURE_THRESHOLD |
5 |
Consecutive failures that open the breaker. | |
SPONGRAM_BREAKER_COOLDOWN_SEC |
60 |
First cool-down. | |
SPONGRAM_BREAKER_MAX_COOLDOWN_SEC |
600 |
Longest cool-down. | |
SPONGRAM_DREAM_ENABLED |
off | on | Starts the Dream. |
SPONGRAM_DREAM_INTERVAL_SEC |
21600 |
21600 |
Seconds between passes. |
SPONGRAM_DREAM_DUP_NAME_SIMILARITY |
0.5 |
0.5 |
Trigram similarity that makes two names candidates. |
SPONGRAM_DREAM_DUP_EMBEDDING_FLOOR |
0.40 |
0.40 |
Minimum cosine of the name embeddings to keep a pair. |
SPONGRAM_DREAM_BACKPROP_MIN_EDGES |
3 |
3 |
Relationships an entity needs to count as active. |
SPONGRAM_DREAM_BACKPROP_WINDOW_HOURS |
24 |
24 |
Window for "recent" relationships. |
SPONGRAM_DREAM_DECAY_TAU_DAYS |
30 |
Time constant of the decay (minimum 1). |
|
SPONGRAM_DREAM_FORGETTING_ENABLED |
off | Marks decayed facts as forgotten. | |
SPONGRAM_DREAM_FORGET_FLOOR |
0.05 |
decayed_score below which a fact is marked. |
|
SPONGRAM_DREAM_RESOLVE_ENABLED |
off | Links same-name entities with SAME_AS. |
SPONGRAM_SLEEPER_ENABLED, SPONGRAM_DREAM_ENABLED and SPONGRAM_DREAM_RESOLVE_ENABLED are
compared exactly: only 1, true and yes, in lower case, turn them on. True or on leaves
the worker off. SPONGRAM_DREAM_FORGETTING_ENABLED accepts the same three values in any case,
and SPONGRAM_PERSONA_SUMMARY_ENABLED also accepts on. The Dream variables are read when the
process starts.
Health: /healthz and /readyz¶
The two endpoints answer different questions, and the workers only appear in one of them.
/readyz answers "can this process serve requests?". It checks the three hard dependencies
(Graphiti, PostgreSQL, the graph database) and nothing else. It returns 200 with
"ready": true, or 503. The workers are deliberately absent.
/healthz (also served as /health) is a diagnostic. In addition to the hard dependencies
it reports sleeper_worker and dream_worker:
| State | Meaning | Healthy |
|---|---|---|
ok |
The loop is alive: a tick is in progress, or the last one is recent enough. | Yes |
starting |
The process started less than 90 seconds ago. | Yes |
throttled |
The worker is skipping ticks because the model's circuit breaker is open. | Yes |
stalled |
The loop ended, a tick has been running for too long, or no tick happened for too long. | No |
not_configured |
The worker is not started. | Desktop edition only |
How "too long" is judged:
- a tick in progress is considered stuck after 900 seconds, or a quarter of the worker's interval if that is longer (90 minutes for a six-hour Dream);
- between ticks, a worker is stale when its last tick ended more than twice its interval ago, or twice the duration of that last tick if it was longer than the interval;
- a stale worker is
throttledinstead ofstalledif it skipped a tick because of the breaker after its last real tick and less than two intervals ago.
The top-level status is ok, starting, throttled, degraded (dependencies fine, a
worker unhealthy) or down (a dependency failing). /healthz returns 503 for degraded and
down.
Warning
Point the container healthcheck and any load-balancer probe at /readyz, never at
/healthz. /healthz turns 503 when a background worker is unhealthy; used as a
healthcheck, it would restart a container that serves requests perfectly well, for a reason
unrelated to its ability to serve. Use /healthz for monitoring and alerting.
On a server a disabled worker is reported as not_configured, which /healthz counts
as unhealthy: with SPONGRAM_SLEEPER_ENABLED or SPONGRAM_DREAM_ENABLED off, /healthz answers
503 with status: degraded permanently, while /readyz is unaffected. Take this into account
in your alerting if you choose to run without one of the workers.
See Observability for the full description of both endpoints and
of /metrics.