Node and edge lifecycle contract (M10, v0.5.0; storage updated in v0.6.0)
Status: frozen contract for v0.5.0 implementation (M10.1). It refines the M10 plan. Deviations from the plan’s proposals are listed in Decisions.
Knotrel remains an exact connectivity engine for undirected simple graphs. Lifecycle support adds registered topology with independent flags, and answers connectivity over the effective graph derived from it. It adds no persistence, typed nodes, multi-graph support or electrical simulation.
1. Model
- Registered node: a node ID present in the topology. It has one flag,
active. - Registered edge: an unordered pair
{u, v}of distinct registered nodes. It has one flag,enabled. - Effective edge: a registered edge that is enabled and whose two endpoints are both active.
- Effective graph: the active nodes and the effective edges.
connectedanswers on this graph only.
Flags describe intended state supplied by the application or source. They are not measured electrical availability.
2. Operations
true from a mutation means one canonical state change; false means no
change. Every validation listed below happens before any mutation, so an
error never leaves a partial change.
| Operation | Effect | No-op (false) |
Errors, in precedence order |
|---|---|---|---|
add_node(n) |
Registers n active and isolated. |
n already registered, active or inactive. It is not reactivated. |
node limit |
link(u, v) |
Registers missing endpoints as active, then registers {u, v} enabled. |
Edge already registered, enabled or disabled. It is not re-enabled. | self-loop; node limit; edge limit (only for a new edge) |
cut(u, v) |
Removes the registered edge, effective or not. | Edge or an endpoint not registered. | self-loop |
set_node_active(n, a) |
Sets the node flag. Incident edges and their enabled flags are kept. |
Flag already a. |
unknown node |
set_edge_enabled(u, v, e) |
Sets the edge flag, also while an endpoint is inactive. | Flag already e. |
self-loop; unknown node (u checked before v); unknown edge |
remove_node(n) |
Removes n and every incident registered edge, including disabled edges and edges to inactive neighbors. |
n not registered. |
none |
connected(u, v) |
Both endpoints are first checked for registration (u, then v). Only then: true iff a path exists in the effective graph; an inactive endpoint gives false, including connected(n, n); an active n gives connected(n, n) = true. |
— | unknown node (u checked before v), before any flag check |
node_active(n) |
Some(active) if registered, else None. |
— | none |
edge_state(u, v) |
Some({enabled, effective}) if registered, else None. u == v returns None in the core; HTTP reports self_loop. |
— | none (core) |
Consequences:
- Reactivating a node restores exactly its incident edges that are enabled and whose other endpoint is active. A disabled edge stays disabled.
- An enabled edge with an inactive endpoint is registered and enabled but not effective.
remove_node(n)followed byadd_node(n)orlink(n, x)creates a fresh active node with no edges. Removed edges never return implicitly. Before a compaction, the new registration reuses the removed node’s retired engine vertex, which is isolated and carries no adjacency (section 5).- Linking to an inactive endpoint registers an enabled edge that is not effective until the endpoint is activated.
3. Counts and limits
| Count | Meaning |
|---|---|
node_count |
Registered nodes, active or inactive. |
edge_count |
Registered edges, enabled or disabled, effective or not. |
active_node_count |
Registered nodes whose flag is active. |
enabled_edge_count |
Registered edges whose flag is enabled. |
effective_edge_count |
Edges in the effective graph. |
max_nodes and max_edges bound registered nodes and edges. Deactivating
or disabling never frees capacity; only remove_node and cut do.
Configured versus enforced limits. LifecycleGraph::limits() returns the
limits as configured. The limits actually enforced are the same, except with
the experimental cluster-forest engine (from 1.1), where they
are at most 2²⁷ − 512 registered nodes and 2²⁸ registered edges whatever is
configured (the engine’s capacity, leaving room for retired slots). A
rejected mutation returns NodeLimitExceeded or EdgeLimitExceeded carrying
the enforced value, and changes nothing.
Without lifecycle support nothing changes: node_count and edge_count keep
their meaning, and the additional counts are not reported.
4. Atomicity, revisions and failure
- Each lifecycle command is one indivisible step under the graph’s exclusive
mutable access (
&mutin Rust, the graph lock in the server). No query observes a partly deactivated or partly removed node. - In the server, one command that returns
changed: trueadvancesstate_versionby exactly one, whatever the number of incident edges. A flag change advances it even when connectivity does not change. No-ops and rejected commands do not advance it. generationchanges only on import publication, never on removal or activation.- Batches keep their contract: ordered, continue after a domain error, no rollback across operations.
- Embedded failure contract: a method that returns
Errmade no change. A panic during a mutation (an internal invariant violation or an allocation failure that unwinds) leaves the graph unspecified; discard it. The server already fails closed on worker panics: the service becomes unavailable and queued work is refused.
5. Engines, complexity and storage
The lifecycle facade (knotrel_core::LifecycleGraph) keeps a catalog of
registered topology and an inner engine. The inner engine is a
ConnectivityGraph created without limits (limits are enforced on the
catalog, so retired slots can never cause NodeLimitExceeded). Its vertices
are every registered node, whatever its flag, plus retired slots; its
edges are exactly the effective edges. No engine state machine is
duplicated and no engine deletion invariant is modified.
Supported engines. Lifecycle support is available with compact-bfs and,
from 1.1, with the experimental cluster-forest (ADR 0014; the
engine sees only the effective graph, rebuilt on compaction, and the enforced
limits of section 3 apply). Lifecycle support for the experimental ett and
hdt engines
is deferred: their resource behavior under lifecycle churn was not
established well enough for this release (see decision 8). Every
LifecycleGraph constructor returns LifecycleError::UnsupportedEngine for
them before examining any input, and the server refuses to start with
lifecycle support and either engine. No engine is ever substituted. The
plain (non-lifecycle) ett and hdt engines, their APIs and their server
configurations are unchanged.
Costs. Let d be the registered degree of the node concerned, d_eff its
effective degree, U the cost of one effective edge insertion or deletion in
the engine (for compact-bfs, O(d_max) because adjacency lists are sorted
vectors; for cluster-forest, O(log² N) amortized by the project’s
derivation, ADR 0014), and V,
E_eff the registered nodes and effective edges at the time of the call.
Hash-map lookups are expected O(1), not worst case.
| Operation | Per call, worst case (hash lookups expected O(1)) |
Amortized over a sequence |
|---|---|---|
set_node_active |
O(d) catalog scan plus d_eff × U |
same |
set_edge_enabled, link, cut |
O(d_u + d_v) catalog work plus at most one U |
same |
remove_node without compaction |
O(d + sum of neighbor degrees) catalog work plus d_eff × U |
same |
remove_node that triggers compaction |
the above plus an O(V log V + E) one-pass engine rebuild from the catalog |
adds O(log V + E / V) rebuild work per removal, with V and E taken at rebuild time |
add_node, or link with a new endpoint, that rebuilds the catalog |
an O(V) rebuild of the catalog table (see “Catalog tombstones”) |
adds O(1) per insertion |
connected |
the engine’s query cost plus two flag checks | same |
Deactivation and removal cut effective incident edges in descending neighbor
order. For compact-bfs this removes from the end of the node’s sorted
adjacency vector, so a high-degree removal does not pay O(d) shifts per
edge.
None of these is logarithmic in general. Removing or deactivating a high-degree hub costs work proportional to its degree, and a compacting removal holds the server’s graph lock for a whole-graph rebuild.
Engine slots and compaction. The engines do not support vertex deletion
and key vertices by node ID. A removed node’s engine vertex stays as an
isolated retired slot: all its edges were cut, and since v0.6.0 its adjacency
allocation is released, so a retired slot keeps only its engine ID-map entry
and an empty vector header. Registering the same ID again reuses that
slot. After every removal, if retired slots exceed max(V, 1024), the facade
rebuilds the engine from the catalog: all registered nodes (active or not) and
all effective edges. More than max(V, 1024) removals separate two rebuilds,
which gives the amortized term above. Engine vertices stay at most
V + max(V, 1024), that is at most 2V + 1024. Each rebuild holds the
server’s graph lock. (v0.6.0 evaluated a max(V / 4, 1024) threshold and
rejected it: fresh-ID churn became about 90% slower.)
Catalog tombstones (since v0.7.0). Removing a node from the catalog
hash table can leave a tombstone that uses insert headroom without holding an
entry. Under fresh-ID churn the headroom runs out while the number of live
nodes stays constant, and the table would double although it is far from full
(at 100,000 nodes, from 131,072 to 262,144 buckets). Before an insert that
would grow the table, the catalog is instead rebuilt into a fresh table sized
for its live entries when they are at most 90% of the table’s full capacity
(its capacity right after its last growth or rebuild). Under churn this is the
same bucket count; it is never larger. Entries move, no incident vector is
copied, and the old table is freed when the rebuild ends. At least
10% of the full capacity is consumed by tombstones between two rebuilds, so
the amortized cost is O(1) per insertion. A rebuild holds the server’s graph
lock, as compaction does; it is a table rebuild only, much shorter than an
engine compaction.
Engine construction. Bulk construction (try_from_topology, flagged
imports) and compaction build the compact-bfs engine in one pass from the
catalog (since v0.6.0): engine indices follow ascending node IDs, each
effective edge is found once from its smaller endpoint, and each adjacency
vector is allocated once with capacity equal to the node’s enabled registered
degree. Index order is internal; no result depends on it.
Memory. Quantities are reported separately and never substituted for one another: live state, container capacity, temporary peaks, allocator retention and RSS.
| Structure | Live state (64-bit) | Retained capacity | Not bounded here |
|---|---|---|---|
| Catalog node map | one 32-byte record per registered node | up to the historical maximum of registered nodes, rounded to the table’s power-of-two growth; under fresh-ID delete/insert churn the table may double once at more than half load | allocator overhead |
| Incident lists | 16 bytes per incidence, two per registered edge | each list at most max(4 × length, 16) elements (shrunk below a quarter) |
— |
| Engine | one ID-map entry and one vector header per engine vertex; 8 bytes per effective incidence | per vertex: at least its historical maximum degree since the last rebuild (cuts do not shrink); after a rebuild, its enabled registered degree, which can exceed its effective degree | — |
| Retired slots | at most max(V, 1024) ID-map entries and vector headers |
— | — |
| Compaction | the old and the new engine are live together during a rebuild, plus an O(V) sorted ID vector |
— | — |
| HTTP import | the request body while it is parsed, then the decoded request, the catalog and the new engine; the old graph stays live until just after publication (atomic replace; it is freed after the graph lock is released). A body that fails the streaming parse is parsed a second time by the former parser to keep the error contract, with that parser’s working set (as in v0.5.0) | — | RSS, allocator retention |
Measured values (allocator byte counts and the qualification results) are in
the v0.6.0 qualification evidence and release notes; they are observations on
one host, not ceilings. Process RSS has no bound from Knotrel: the allocator
may retain freed pages. (Independently of lifecycle support, the ett and
hdt engines free an incidence set’s storage when it becomes empty, and hdt
releases an upper level’s record of a vertex that becomes isolated there,
reusing the slot.)
Hash-table capacity under churn (v0.6.0)
This bound follows from the implementation of std::collections::HashMap
(the hashbrown SwissTable; rule checked in the hashbrown 0.17.1 source). It is
an implementation property, not a documented std guarantee, and is stated for
64-bit targets.
Variables. B is the table’s bucket count (a power of two); its capacity
is cap(B) = 7B/8 for B ≥ 16. n is the number of entries; N is the
largest n the table ever holds. The catalog’s node map stores a 40-byte
entry (8-byte key, 32-byte record) plus one control byte per bucket.
Rule. Removing an entry turns its slot into an empty slot or a tombstone.
When an insertion finds no free capacity left (live entries plus tombstones),
the table either rehashes in place at the same B when n + 1 ≤ cap(B) / 2,
which clears every tombstone, or grows to the smallest power of two whose
capacity is at least max(n + 1, cap(B) + 1), which doubles B.
Bound. Growth happens only while cap(B) / 2 < N + 1. Therefore B never
exceeds the larger of its initial value and the smallest power of two with
cap(B) ≥ 2(N + 1). The number of removals, insertions, compactions or
re-registrations does not enter the bound; only N does.
Consequences for each churn pattern:
- Fresh-ID churn at a constant registered count
V(remove a node, register a never-used ID): tombstones accumulate, so a table that was allocated more than half full doubles once and then only rehashes in place. ForV = 100,000, import sizes the table atB = 131,072(capacity 114,688). It grows toB = 262,144(capacity 229,376 ≥ 200,002), about 5.1 → 10.3 MiB of allocated table memory, observed between cycles 3 and 6 of the diagnostic. Whether the doubling happens depends on whereVfalls between powers of two: a table less than half full never doubles. - Same-ID reuse (remove and re-register the same ID): same rule. The count does not grow, so at most the same single doubling can occur.
- Toggling flags inserts and removes nothing in the map: no growth.
- Compaction rebuilds the engine, not the catalog. The catalog map keeps
its capacity. The engine’s ID map (16-byte entries) holds up to
2V + 1024entries before a compaction rebuilds it at capacityV, so the same rule bounds it by the table for2V + 1024entries.
Capacity, allocated memory and RSS are different quantities. Capacity is
the entry count above. Allocated memory is about 41 × B bytes for the
catalog map, plus each record’s incident list, which this bound does not
cover. RSS also includes pages the allocator keeps after the old table is
freed, and is not bounded by Knotrel. The qualification’s heap-growth check
(C3) counts allocated memory over 30 cycles. A one-time doubling inside that
window therefore raises the check’s slope even though it is bounded; the
check’s verdict stands as measured.
6. Rust API (knotrel-core)
Additive; Graph, ConnectivityGraph and GraphError are unchanged.
pub struct LifecycleGraph { /* private */ }
pub struct NodeSpec { pub node: NodeId, pub active: bool }
pub struct EdgeSpec { pub source: NodeId, pub target: NodeId, pub enabled: bool }
#[non_exhaustive] // from 1.0; build expected values with EdgeState::new
pub struct EdgeState { pub enabled: bool, pub effective: bool }
#[non_exhaustive]
pub enum LifecycleError {
Graph(GraphError),
UnknownEdge { source: NodeId, target: NodeId },
ConflictingNode { node: NodeId },
ConflictingEdge { source: NodeId, target: NodeId },
UnsupportedEngine { engine: EngineConfig },
}
impl LifecycleGraph {
pub const fn supports_engine(engine: EngineConfig) -> bool; // compact-bfs; cluster-forest from 1.1
pub fn new(engine: EngineConfig) -> Result<Self, LifecycleError>;
pub fn with_limits(engine: EngineConfig, limits: GraphLimits)
-> Result<Self, LifecycleError>;
pub fn try_from_topology(engine, limits, nodes: &[NodeSpec], edges: &[EdgeSpec])
-> Result<Self, LifecycleError>;
pub fn add_node(&mut self, node) -> Result<bool, GraphError>;
pub fn link(&mut self, source, target) -> Result<bool, GraphError>;
pub fn cut(&mut self, source, target) -> Result<bool, GraphError>;
pub fn set_node_active(&mut self, node, active: bool) -> Result<bool, LifecycleError>;
pub fn set_edge_enabled(&mut self, source, target, enabled: bool)
-> Result<bool, LifecycleError>;
pub fn remove_node(&mut self, node) -> bool;
pub fn connected(&self, source, target) -> Result<bool, GraphError>;
pub fn node_active(&self, node) -> Option<bool>;
pub fn edge_state(&self, source, target) -> Option<EdgeState>;
pub fn node_count / edge_count / active_node_count / enabled_edge_count
/ effective_edge_count(&self) -> usize;
pub fn engine_vertex_count(&self) -> usize; // includes retired slots
pub fn storage_stats(&self) -> LifecycleStorage; // O(V): sizes and capacities
pub fn limits(&self) -> GraphLimits;
pub fn engine(&self) -> EngineConfig;
}
new, with_limits and try_from_topology return
UnsupportedEngine { engine } for ett and hdt; try_from_topology checks
the engine before any entry. try_from_topology registers nodes, then
edges (creating missing endpoints active). Repeated entries with identical flags are accepted; a node
or edge repeated with a different flag returns ConflictingNode or
ConflictingEdge (edges compare as unordered pairs). Self-loops and limits
return LifecycleError::Graph.
7. Server opt-in
KNOTREL_TOPOLOGY_LIFECYCLE is exactly true or false (default false),
also ServerConfig::with_topology_lifecycle. With false, the server holds
the plain engine as before and no catalog is allocated; every engine and mode
behaves as in v0.4.0 apart from the additive changes listed in the release
notes (lifecycle request names, events and flagged import entries get 409
topology_lifecycle_disabled instead of 422, and one more feeder-rejection
series).
true requires KNOTREL_ENGINE=compact-bfs (the default) or, from 1.1,
cluster-forest. With ett or hdt, ServerConfig::from_env
fails with “KNOTREL_TOPOLOGY_LIFECYCLE requires
KNOTREL_ENGINE=compact-bfs or cluster-forest; node and edge lifecycle
support for <engine> is not available in this release”, so the executable exits with
status 1 before binding or serving. Embedded callers get the same
ConfigError from ServerConfig::with_topology_lifecycle(true), which
returns Result<ServerConfig, ConfigError>; the engine is fixed by
ServerConfig::new, so no router can be built with an unsupported
combination.
GET /v1/info adds "topology_lifecycle": true only when enabled.
Direct operations (/v1/operations, /v1/batch)
New operation objects (all fields required, no others allowed):
{"op":"set_node_active","node":"202","active":false}
{"op":"set_edge_enabled","source":"201","target":"202","enabled":false}
{"op":"remove_node","node":"202"}
{"op":"node_state","node":"202"}
{"op":"edge_state","source":"201","target":"202"}
Results: mutations return {"result":"changed","changed":bool}. Queries:
{"result":"node_state","registered":true,"active":false}
{"result":"edge_state","registered":true,"enabled":true,"effective":false}
An absent object reports registered: false and false flags; it is not an
error. edge_state with source == target returns self_loop.
New error codes: unknown_edge (HTTP 404 for a single operation) and
topology_lifecycle_disabled (HTTP 409). unknown_node keeps HTTP 404.
Request validation order. The steps of the HTTP request validation order are extended as follows; the first failing step decides the response:
- Step 5 (shape, 422
invalid_request) always knows the five new operation shapes, whatever the setting. A legacy operation carryingactiveorenabledfails here as an unknown field. - New step 5a (409
topology_lifecycle_disabled): lifecycle support is off and the single operation, or any operation of a batch, is one of the five new operations. - Step 6 (409
direct_mutation_disabled): in source-managed mode the mutation list becomesadd_node,link,cut,set_node_active,set_edge_enabledandremove_node.node_stateandedge_stateare queries: source-managed query-only batches may containconnected,node_stateandedge_state, with the same readiness rules asconnected. - Steps 7–9 are unchanged.
Feeder events (/v1/feeder/events)
New op values, with the existing envelope fields:
op |
Required | Forbidden |
|---|---|---|
set_node_active |
node, active |
source, target, enabled |
set_edge_enabled |
source, target, enabled |
node, active |
remove_node |
node |
source, target, active, enabled |
progress, link, cut and add_node must not carry active or
enabled: 422 invalid_request, whatever the setting. A flag whose value is
null is a shape error (422) wherever a flag is accepted, so it is never
treated as absent. Feeder set_edge_enabled checks endpoints in the supplied
order, as direct operations do; only the duplicate fingerprint is
normalized.
With lifecycle support off, an event using one of the three new op values
rejects the whole request with 409 topology_lifecycle_disabled. The check
runs in the existing per-event prevalidation slot, under the graph lock after
session, generation and source checks and before any event is applied, in
event order together with invalid_request (the first failing event
decides). Within one event, a known lifecycle op without lifecycle support
is rejected with 409 before its fields are checked; an unknown op is still
422 invalid_request. It is counted in knotrel_feeder_rejections_total with
code="topology_lifecycle_disabled", a new value of that closed label set.
The canonical operation used for duplicate detection includes the operation
type, normalized endpoints and the flag value. A retry with the same
delivery_sequence and event_id but a different flag or operation is a
conflicting duplicate. Domain errors follow the existing feeder failure
matrix: HTTP 422, a plain error with the domain code (unknown_node,
unknown_edge or self_loop) for a single-event request, and
partial_failure with outer code event_failed for a multi-event request.
Feeder events keep having no 404 outcome. remove_node
of an absent node is an applied no-op.
Stale-event safety comes from the existing ordering and fencing: a retained
duplicate is answered from the window without being applied, an evicted one is
unverifiable_duplicate, and a different sequence must be the next in order.
No event can resurrect removed topology except a new in-order add_node or
link.
Import (/v1/feeder/import)
nodesentries are either a node ID string (active) or an object{"node":"202","active":false}.edgesentries accept an optional"enabled": false(defaulttrue).- The imported graph contains every registered node and edge with its flags, including isolates, inactive nodes and disabled edges.
- Repeated entries must agree; a conflict is
invalid_nodeorinvalid_edge(HTTP 422). Legacy payloads are unchanged and mean all active, all enabled. - With lifecycle support off, any node object or any
enabledmember rejects the import withtopology_lifecycle_disabled(HTTP 409), even when the value is the default. The check belongs to the payload phase: after JSON parsing and theinstance_id/graph_idchecks, before the candidate is built and before the graph lock is requested. It is classified aspayload_rejectedinknotrel_imports_total. - The candidate is built off-lock and published atomically, as before.
Stats, import response and metrics
/v1/statsand the import response keepnode_countandedge_count(registered). With lifecycle support they addactive_node_count,enabled_edge_countandeffective_edge_count.remaining_*use registered counts.knotrel_graph_nodesandknotrel_graph_edgesreport registered counts. With lifecycle support only, three gauges are added:knotrel_graph_active_nodes,knotrel_graph_enabled_edgesandknotrel_graph_effective_edges.knotrel_graph_operations_totalgainsopvaluesset_node_active,set_edge_enabled,remove_node(outcomeschanged,unchanged,error),node_stateandedge_state(outcomesregistered,unregistered,error). Their series exist only with lifecycle support and, as for other mutations, only in standalone mode for the mutations.node_stateandremove_nodecannot fail, so theirerrorseries exist for uniformity but never increment.- The three new counts are maintained in
O(1)per mutation by the core and read only when metrics are enabled, at the existing per-job publication boundary (observability contract section 10). With metrics disabled, or with lifecycle support off, no additional telemetry work is done. - No label carries a node ID, edge, source cursor or event ID.
Decisions and deviations
- Separate facade, not a mode of
ConnectivityGraph. Keeps the default path free of lifecycle bookkeeping and leavesConnectivityGraph’s behavior and cost unchanged. - Separate
LifecycleError(non-exhaustive) instead of newGraphErrorvariants, so existing exhaustive matches still compile. - Uniform retired-slot strategy with amortized rebuild instead of per-engine vertex deletion. ETT and HDT deletion invariants are not modified.
- Absent objects in state queries are not errors (
registered: false), unlikeconnected, which keepsunknown_node. - Whole-request rejection when lifecycle support is off, consistent with
direct_mutation_disabled, rather than per-item batch errors. - Flags in import reject when lifecycle support is off even at the default value, so a sender cannot assume flags were honored.
- Contract review (2026-11-16). An independent review found no blockers.
Its major findings were resolved in this text:
connectedchecks registration before flags; the HTTP, feeder and import positions oftopology_lifecycle_disabledare fixed; feeder domain errors keep 422; the cost table separates worst-case, compaction spikes and amortized terms; the memory statement is an estimate pending measurement. - Lifecycle support limited to
compact-bfs(owner decision B1, 2026-11-16). The qualification of earlier candidates found, onettandhdtwith lifecycle, removal regressions at high degree, heap growth failures, and forhdtretained storage that kept growing through 120 cycles on one replayed path without an established practical steady state (no leak was demonstrated; per-level arenas are bounded by code but no practical bound for the whole process was established). Rather than accept those costs,ettandhdtlifecycle support is deferred. The rejection is explicit in the core constructors and at server configuration, never a silent engine switch. Those earlier results remain recorded for the candidates they measured; the exclusion does not turn them into passes.