Circuit¶
The knowledge graph engine -- FSRS scheduling + NetworkX graph + propagation + sqlite-vec.
Circuit ¶
Circuit(db_path: str | Path = DEFAULT_DB_PATH, plasticity: Plasticity | None = None, embedder: Embedder | None = None, read_only: bool = False)
The knowledge graph engine — FSRS scheduling + NetworkX graph + propagation.
Circuit is the main entry point for spikuit-core. It owns the database, the in-memory NetworkX graph, and exposes all operations that external layers (CLI, agents, sessions) need.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str | Path
|
Path to the SQLite database file. |
DEFAULT_DB_PATH
|
plasticity
|
Plasticity | None
|
Tunable learning parameters (uses defaults if |
None
|
embedder
|
Embedder | None
|
Embedding provider for semantic search (optional). |
None
|
current_transaction
property
¶
Return the active transaction, if any. Adapter-only API.
transaction
async
¶
transaction(*, tag: str | None = None, actor_id: str, actor_kind: ActorKind = 'agent') -> AsyncIterator[SpikuitTransaction]
Open an explicit changeset.
All mutations performed inside the block (in v0.7.0+ commits) are buffered as events and flushed atomically on exit. Raising an exception aborts the changeset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
str | None
|
Caller-supplied label, e.g. "ingest:papers-2026". |
None
|
actor_id
|
str
|
Free-form identifier of who initiated the change. |
required |
actor_kind
|
ActorKind
|
One of "human", "agent", "system". |
'agent'
|
Raises:
| Type | Description |
|---|---|
TransactionNestingError
|
If a transaction is already active on this Circuit (nested transactions are not supported in v0.7.0). |
add_neuron
async
¶
remove_neuron
async
¶
Soft-retire a neuron and cascade-retire its synapses.
The neuron row stays in the database with retired_at set,
preserving FSRS state and history. Its vector row is physically
deleted to keep ANN recall undegraded. Synapses touching the
neuron are cascade-retired. A neuron.retire event plus one
synapse.retire event per cascaded synapse are emitted in
the current (or implicit) transaction.
add_quiz_item
async
¶
Persist a quiz item with neuron associations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
QuizItem
|
The quiz item to store. Must have at least one neuron in
|
required |
Returns:
| Type | Description |
|---|---|
QuizItem
|
The persisted QuizItem (with auto-generated ID if empty). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no primary neuron is specified. |
get_quiz_items
async
¶
get_quiz_items(neuron_id: str, *, role: QuizItemRole | None = None, scaffold_level: ScaffoldLevel | None = None) -> list[QuizItem]
Get quiz items associated with a neuron.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron_id
|
str
|
The neuron to look up. |
required |
role
|
QuizItemRole | None
|
Filter by role (primary/supporting). |
None
|
scaffold_level
|
ScaffoldLevel | None
|
Filter by scaffold level. |
None
|
Returns:
| Type | Description |
|---|---|
list[QuizItem]
|
List of matching QuizItems, newest first. |
add_synapse
async
¶
add_synapse(pre: str, post: str, type: SynapseType, weight: float = 0.5, confidence: SynapseConfidence = SynapseConfidence.EXTRACTED, confidence_score: float = 1.0) -> list[Synapse]
Add a Synapse between two neurons.
Bidirectional types (contrasts, relates_to) automatically
create the reverse edge as well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pre
|
str
|
Source neuron ID. |
required |
post
|
str
|
Target neuron ID. |
required |
type
|
SynapseType
|
Connection semantics. |
required |
weight
|
float
|
Initial edge weight (default |
0.5
|
confidence
|
SynapseConfidence
|
Provenance tag (EXTRACTED, INFERRED, AMBIGUOUS). |
EXTRACTED
|
confidence_score
|
float
|
Confidence score (0.0–1.0, meaningful for INFERRED). |
1.0
|
Returns:
| Type | Description |
|---|---|
list[Synapse]
|
List of created synapses (1 for directed, 2 for bidirectional). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either neuron does not exist in the circuit. |
remove_synapse
async
¶
Soft-retire a synapse and emit a retire event.
Bidirectional types retire both directions.
list_synapses
async
¶
List synapses, optionally filtered by neuron or type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron_id
|
str | None
|
If given, return synapses where this neuron is pre or post. |
None
|
type
|
SynapseType | None
|
If given, filter to this synapse type. |
None
|
Returns:
| Type | Description |
|---|---|
list[Synapse]
|
List of matching synapses. |
set_synapse_weight
async
¶
Set the weight of an existing synapse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pre
|
str
|
Source neuron ID. |
required |
post
|
str
|
Target neuron ID. |
required |
type
|
SynapseType
|
Synapse type. |
required |
weight
|
float
|
New weight value. |
required |
Returns:
| Type | Description |
|---|---|
Synapse
|
The updated Synapse. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the synapse does not exist. |
merge_neurons
async
¶
Merge multiple neurons into a target neuron.
Content from source neurons is appended to the target. Synapses are redirected, source links transferred, and source neurons removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_ids
|
list[str]
|
IDs of neurons to merge (will be deleted). |
required |
into_id
|
str
|
ID of the target neuron (survives). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Summary dict with merge statistics. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any neuron does not exist or into_id is in source_ids. |
predecessors_of_lineage
async
¶
Return parent neuron IDs recorded when neuron_id absorbed them.
Adapter-only read API for AMKB L2 lineage conformance.
prune_retired
async
¶
Physically delete all soft-retired neurons and synapses.
Escape hatch for spkt history prune. Event log is preserved.
Does not emit events — this is historical garbage collection,
not a lifecycle transition.
upsert_meta_neuron
async
¶
Create or replace a _meta domain neuron.
_meta neurons are auto-generated descriptions of the Brain itself. They participate in retrieve() but are excluded from due/fire.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meta_id
|
str
|
The neuron ID (e.g. |
required |
content
|
str
|
Markdown content for the neuron. |
required |
Returns:
| Type | Description |
|---|---|
Neuron
|
The created or updated Neuron. |
clear_meta_neurons
async
¶
Remove all _meta domain neurons.
Returns:
| Type | Description |
|---|---|
int
|
Number of neurons removed. |
generate_manual
async
¶
Generate a user guide for this Brain.
Returns a dict with domain overview, sample topics, knowledge cutoff, coverage notes, and source attribution. Optionally writes _meta neurons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
write_meta
|
bool
|
If True, upsert _meta neurons with manual content. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
Dict with keys: domains, cutoff, coverage, sources, neuron_count. |
fire
async
¶
Record a review event, update FSRS state, and propagate activation.
This is the central method for all review operations. The full pipeline is:
- Record spike to DB
- FSRS: update stability, difficulty, schedule next review
- APPNP: propagate activation to neighbors (pressure deltas)
- Reset source neuron pressure
- STDP: update edge weights based on co-fire timing
- Record last-fire timestamp for future STDP
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spike
|
Spike
|
The review event to process. |
required |
Returns:
| Type | Description |
|---|---|
Card
|
The updated FSRS Card with new scheduling state. |
get_card ¶
Get the FSRS Card for a neuron (from in-memory cache).
due_neurons
async
¶
Return neuron IDs that are due for review.
near_due_neurons
async
¶
near_due_neurons(*, days_ahead: int = 2, limit: int = 20, exclude_ids: set[str] | None = None, now: datetime | None = None) -> list[str]
Return neuron IDs whose next review is within days_ahead days
but not yet due. Used by interleaving to pull near-due work from
other domains without breaking FSRS optimality significantly.
decay_pressure ¶
Apply LIF leak to all neuron pressures.
commit_retrieval_boosts
async
¶
Persist all in-memory retrieval boosts to DB.
retrieve
async
¶
Retrieve neurons matching a query with graph-weighted scoring.
Scoring formula::
score = max(keyword_sim, semantic_sim)
× (1 + retrievability + centrality + pressure + boost)
semantic_sim uses sqlite-vec KNN when an embedder is configured;
otherwise only keyword matching is used. boost is accumulated
through QABotSession feedback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query text. |
required |
limit
|
int
|
Maximum number of results. |
10
|
filters
|
dict[str, str] | None
|
Key-value filters. |
None
|
Returns:
| Type | Description |
|---|---|
list[Neuron]
|
List of matching neurons, sorted by score descending. |
ensemble ¶
Get the N-hop neighborhood of a neuron.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron_id
|
str
|
Center neuron. |
required |
hops
|
int
|
Radius of the ego graph (default 2). |
2
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of neighbor neuron IDs (excluding the center). |
embed_all
async
¶
Backfill embeddings for all neurons that don't have one yet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of texts to embed per API call. |
32
|
Returns:
| Type | Description |
|---|---|
int
|
Number of neurons newly embedded. |
add_source
async
¶
Add a Source to the circuit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Source
|
The source to persist. |
required |
Returns:
| Type | Description |
|---|---|
Source
|
The same source (pass-through for chaining). |
attach_source
async
¶
Link a source to a neuron (idempotent).
detach_source
async
¶
Remove the link between a neuron and a source.
update_source
async
¶
Update source fields (pass the modified Source object).
get_neurons_for_source
async
¶
Get neuron IDs attached to a source.
get_meta_keys
async
¶
Get distinct filterable/searchable keys with counts.
get_meta_values
async
¶
Get distinct values for a metadata key.
get_stale_sources
async
¶
Get URL sources older than stale_days since last fetch.
rename_domain
async
¶
Rename all neurons with domain=old to domain=new.
merge_domains
async
¶
Merge multiple domains into target.
detect_communities
async
¶
Run Louvain community detection and persist results.
Uses an undirected projection of the graph. Results are stored in the DB and loaded into NetworkX node data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution
|
float
|
Louvain resolution parameter. Higher values produce more communities. |
1.0
|
Returns:
| Type | Description |
|---|---|
dict[int, list[str]]
|
Mapping of community_id → list of neuron IDs. |
get_community ¶
Get the community ID for a neuron (from in-memory graph).
community_map ¶
Return a mapping of neuron_id → community_id for all assigned neurons.
generate_community_summaries
async
¶
Generate summary neurons for each community.
For each community, creates a community_summary neuron with
member titles and domain info, linked to members via summarizes
synapses. Replaces existing summaries on re-run.
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of dicts with summary neuron info per community. |
consolidate
async
¶
consolidate(*, decay_factor: float = 0.8, weight_floor: float | None = None, similarity_threshold: float = 0.85, domain: str | None = None) -> dict
Generate a consolidation plan (dry-run).
Biologically-inspired 4-phase consolidation: 1. SWS (Replay): Discover latent synapses via embedding similarity 2. SHY (Downscaling): Decay weights, identify prunable synapses 3. REM (Interference): Detect near-duplicate neurons 4. Triage: Flag low-value neurons as forget candidates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decay_factor
|
float
|
Multiply all synapse weights by this (SHY phase). |
0.8
|
weight_floor
|
float | None
|
Prune synapses below this. Defaults to plasticity.weight_floor. |
None
|
similarity_threshold
|
float
|
Cosine similarity threshold for latent synapses / duplicates. |
0.85
|
domain
|
str | None
|
Optional domain filter (TMR-inspired targeted consolidation). |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
A consolidation plan dict with actions and state_hash. |
apply_consolidation
async
¶
Apply a consolidation plan. Validates state hash first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plan
|
dict
|
A plan dict from consolidate(). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Summary of applied actions. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the current graph state doesn't match the plan's hash. |
diagnose
async
¶
Run read-only brain health diagnostics.
Returns a structured dict with all health metrics: orphans, weak_synapses, domain_balance, community_cohesion, bridge_gaps, dangling_prerequisites, source_freshness, surprise_bridges.
domain_audit
async
¶
Analyze domain ↔ community alignment and suggest actions.
Compares the user-assigned domain labels against the graph's natural community structure (Louvain) to find mismatches:
- split: a domain spans multiple communities → suggest sub-domains
- merge: multiple domains converge in one community → suggest merging
- rename: keyword extraction hints at a better name
Returns a dict with domain_map, community_map, suggestions[].
progress
async
¶
Generate a learner-focused progress report.
Returns per-domain mastery, retention rate, learning velocity, weak spots, and review adherence.