External source-of-truth integration contract
Status: Proposed (Candidate Design for Review — Awaiting Maintainer Approval).
Cross-reference: Normative Integration Contract.
Context
Knotrel v0.1.0 provides an in-memory dynamic connectivity accelerator for a single undirected graph. All mutations (POST /v1/operations, POST /v1/batch) apply directly to memory. State is strictly volatile: restart loses all graph data.
In production architectures, an external database or transactional event stream remains the authoritative source of truth. Knotrel must serve as a derived read accelerator. To prevent data corruption and divergence, Knotrel must never claim that applying an update in memory makes it durable in the authoritative source. Furthermore, untracked direct client writes must not bypass source ordering, and process restart must trigger a structured rebuild from the external source rather than presenting an empty graph as synchronized.
Feature 0.2.1 establishes the architectural contract for v0.2.0 (Integration & Graph Loading) prior to any runtime implementation.
Decision
1. Two Operating Modes
- Standalone Volatile Mode (Default): Retains 100% compatibility with v0.1.0. Direct mutations via
/v1/operationsand/v1/batchare accepted. Readiness transitions toreadyat startup. - Source-Managed Mode (Opt-In): Explicitly selected at startup (
KNOTREL_MODE=source-managed). Direct mutating operations (add_node,link,cut) and mutating batches are rejected with HTTP 409 Conflict (direct_mutation_disabled). Connectivity queries remain supported. Readiness is false until verified synchronization. Restart discards state and requires full external rebuilding.
2. Identity, Instance Scoping, and Fencing
- Instance ID: Ephemeral UUID generated on startup; changes on every restart. Required in
X-Knotrel-Instance-Idacross all feeder requests. Requests bearing a staleinstance_idare rejected immediately with HTTP 409 (stale_instance_id), preventing cross-restart generation collisions. - Graph ID: Single implicit logical graph identifier (
"default"). - Generation: Monotonic 64-bit integer ($1, 2, \dots$) incremented on each atomic graph import. Updates must match the active generation; older generations are rejected (
stale_generation). - State Version: Preserves v0.1.0 arbitrary-precision decimal string. Increments only on topological changes (
changed: true). Accepted no-op events advance source position without incrementingstate_version. - Source Lineage:
source_idandsource_epochbound to the generation. Source failover or epoch changes reject updates (source_epoch_mismatch) and require a full rebuild. - Feeder Lease Protocol: Explicit session registration (
POST /v1/feeder/session) and periodic heartbeats (POST /v1/feeder/heartbeat) govern active feeder ownership. HTTP connection drops do not expire ownership; monotonic lease expiration (proposed default 10s timeout) transitions state toStale. Higherfeeder_epochfences stale feeders (fenced_feeder_session). Heartbeats renew the lease deadline but cannot restoreReadywithout synchronization proof.
3. Synchronization Protocol ($S \to \text{Catch-up} \to B$)
- Image Import ($S$): The feeder delivers a coherent baseline graph image at source position $S$. Mode is explicitly
create_onlyorreplace. Offline candidate build is bounded by a proposed 64 MiB payload limit and a concurrency ceiling of 1 (KNOTREL_MAX_CONCURRENT_IMPORTS = 1). Atomic swap under graph mutex establishes generation $g$ and entersCatchingUp.- Error Segregation on Import: Calling
create_onlywhengeneration > 0returns409 Conflict(graph_already_initialized). Forreplace, composite precondition mismatch onIf-Match: "<gen>.<state_version>.<percent_encoded_source_position>"returns412 Precondition Failed(precondition_failed). Fencing or lease failures return409 Conflict(fenced_feeder_session,lease_expired).
- Error Segregation on Import: Calling
- Verifiable Continuity & Execution Boundary: External cursors are treated as opaque strings. The feeder provides a transport continuity envelope: contiguous
delivery_sequence($1, 2, 3, \dots$),prev_delivery_sequence, and predecessor cursorprev_source_position. - Ordered Progress Events: Source progress without graph mutations is advanced solely through ordered, deduplicated
progressevents (op: "progress"). - Mutually Exclusive Barrier Evaluation Table ($B$): The feeder declares barrier $B$ via
POST /v1/feeder/barrier. Barriers are strictly verification-only: they assert that the stream has applied updates and progress up to $B$ ($D_B, P_B$) and never mutate topology or advance cursors.- Evaluation requires valid feeder ownership (
feeder_session_id,feeder_epoch), an active unexpired monotonic lease, matchinginstance_id, and matchinggeneration(mismatches return409 Conflictwhile preserving state). - Mutually exclusive decision rules:
- Exact Match ($D_B == D_{applied} \land P_B == P_{applied}$):
- If
CatchingUporStale(published $g > 0$), returns200 OK(result: "synchronized") and restoresReady(Case B1). - If already
Ready(published $g > 0$), returns200 OK(result: "already_synchronized") and remainsReady(Case B2). - If
Uninitialized($g == 0$), returns409 Conflict(code: "graph_uninitialized") and remainsUninitialized(Case B3; no barrier may make an unpublished initial graphReady). - If initial baseline
Loading($g == 0$), returns409 Conflict(code: "graph_uninitialized") and remainsLoading(Case B4). - If
Desynchronized, returns409 Conflict(code: "graph_desynchronized") and remainsDesynchronized(Case B5; barrier cannot clear gap). - If
Failed, returns503 Service Unavailable(code: "unavailable") and remainsFailed(Case B6).
- If
- Position Mismatch at Equal Sequence ($D_B == D_{applied} \land P_B \ne P_{applied}$): Returns
409 Conflict(code: "barrier_position_mismatch") and preserves state (Case B7). - Premature Sequence ($D_B > D_{applied}$): Returns
409 Conflict(code: "barrier_not_reached") and preserves state (Case B8). - Older Sequence ($D_B < D_{applied}$): Conservatively rejected with
409 Conflict(code: "superseded_barrier") and preserves state (Case B9). Because Knotrel maintains only the current stream frontier $(D_{applied}, P_{applied})$ and recent operation descriptors in its ring buffer without retaining an unbounded historical position ledger, historical matches cannot be proven. Conservative rejection prevents delayed or reordered barriers from regressing or altering synchronization state.
- Exact Match ($D_B == D_{applied} \land P_B == P_{applied}$):
- Prohibition of Barriers Making Unpublished Graphs Ready: No barrier can make an unpublished graph
Ready. InUninitializedand initial baselineLoading($g == 0$), all barrier attempts are rejected with409 graph_uninitialized. - Relationship Between Replacement Loading and Published Graph: While a replacement candidate $g+1$ is constructed off-lock, the live published graph remains active at generation $g$. Barriers for generation $g$ evaluate against generation $g$’s stream frontier normally. Barriers for candidate $g+1$ are rejected with
409 Conflict(code: "stale_generation"), preserving generation $g$’s live state. Only upon atomic swap under lock is generation $g+1$ published. - State Preservation Invariant: Every rejected barrier (B3 through B9) strictly preserves the existing synchronization state.
- Evaluation requires valid feeder ownership (
- Sequence Classification and Dual-Index Deduplication under Mutex: Request sequence is evaluated under the graph mutex using a bounded window of capacity $N = 10{,}000$ spanning $[D_{window_min}, D_{applied}]$:
- Previous Sequence ($D \le D_{applied}$):
- If evicted ($D < D_{window_min}$): returns
409 Conflict(code: "unverifiable_duplicate"); state unchanged. - If in window ($D \in [D_{window_min}, D_{applied}]$):
- Retained sequence with different event ID (
req.event_id != retained.event_id): returns409 Conflict(code: "conflicting_duplicate") $\to$ transitions state toDesynchronized. - Retained sequence with matching event ID: Tier 2 canonical check. If canonical content matches, returns
200 OK(status: "already_applied"). If canonical content differs, returns409 Conflict(code: "conflicting_duplicate") $\to$ transitions state toDesynchronized.
- Retained sequence with different event ID (
- If evicted ($D < D_{window_min}$): returns
- Next Sequence ($D == D_{applied} + 1$):
- Predecessor sequence check verifies $D_{prev} == D_{applied}$. Predecessor position check verifies
event.prev_source_position == live.applied_source_position(mismatch returns409 Conflict(code: "source_predecessor_mismatch") $\to$Desynchronized). - Event ID uniqueness check in window: if
req.event_idexists in the retainedevent_idhash index, returns409 Conflict(code: "conflicting_duplicate") $\to$ transitions state toDesynchronized. - If not in window: executes mutation or progress event. On success, increments
state_version(if changed), advancesapplied_source_positionand $D_{applied}$, and records entry in ring buffer andevent_idhash index (evicting oldest entry if $> N$). - Evicted history boundary: if an incoming event at $D_{applied} + 1$ reuses an
event_idfrom an evicted sequence ($< D_{window_min}$), the bounded window cannot detect the historical reuse; it evaluates as fresh. Detection guarantees hold strictly within the retained $N$-entry window.
- Predecessor sequence check verifies $D_{prev} == D_{applied}$. Predecessor position check verifies
- Future Sequence ($D > D_{applied} + 1$): Rejected as
409 Conflict(code: "out_of_order_sequence"). Live state remains at $D_{applied}$.
- Previous Sequence ($D \le D_{applied}$):
- Rejected Events vs. Committed Events:
- The deduplication window records only successfully committed events ($D \le D_{applied}$).
- An uncommitted rejected attempt (e.g. failing domain validation like
self_loopor capacity limit rejection) is not recorded in the deduplication window. Retrying at sequence $D_{applied} + 1$ with corrected payload is evaluated fresh and can succeed without triggeringconflicting_duplicate. - In contrast, once an event is committed ($D \le D_{applied}$), any reuse of its sequence with a different event ID, reuse of its event ID at a new sequence, or alteration of its canonical content triggers
409 Conflict(code: "conflicting_duplicate") $\to$Desynchronized.
- Progress Invariants on Failed Mutations:
- A
progressevent is permitted only when the corrected authoritative source projection requires no graph mutation. - Feeder is strictly forbidden from substituting a
progressevent to skip or bypass a required graph mutation that failed (e.g., due to capacity limits such asnode_limit_exceededoredge_limit_exceeded). Bypassing a required mutation causes silent, unrecoverable topological divergence. - Required mutation failures require halting stream progress and executing either operator capacity reconfiguration followed by full external rebuild, or declaring an unrepairable source gap via
POST /v1/feeder/gap.
- A
- Declared Gap: Irreparable source loss declared via
POST /v1/feeder/gapreturns200 OK(result: "gap_acknowledged") and transitions state toDesynchronized.
4. Readiness and Freshness
- Mode-specific readiness states:
Uninitialized,Loading,CatchingUp,Ready(Synchronized),Disconnected/Stale,Desynchronized,Failed. - Queries executed in source-managed mode expose coherent metadata headers (
X-Knotrel-Instance-Id,X-Knotrel-Generation,X-Knotrel-State-Version,X-Knotrel-Source-Position,X-Knotrel-Sync-Status). - A graph known to be divergent or lagging past an unrepairable gap is never reported as synchronized.
5. Roadmap Decisions D1–D6 Resolution
- D1 (Identity): Single implicit graph with
instance_idheader validation, monotonicgeneration, andfeeder_epochlease fencing. - D2 (Modes & Ack): Strict startup segregation via
KNOTREL_MODE. Direct writes rejected in source-managed mode (direct_mutation_disabled). Ack certifies volatile memory application only. - D3 (Ordering & Retry): Feeder-supplied
delivery_sequenceandprev_source_positionwith sequence classification, dual-index bounded deduplication under the mutex (sequence ring buffer andevent_idhash index), and verification-only barriers with a complete 9-case decision table. Conflicting duplicate detection covers sequence reassignment, event ID reuse, and canonical content alterations within the retained $N$-entry horizon, while evicted sequences return409 unverifiable_duplicate. Uncommitted rejected attempts are not recorded in the deduplication window and may be retried at $D_{applied}+1$. Progress events cannot bypass failed required mutations. Deduplication memory accounting requires explicit tracking of ring buffer storage, hash index buckets, string heap allocations (max 64 Bevent_id, max 256 Bsource_position), canonical payload descriptors, and allocator overhead for the proposed 10,000-entry window without invented numerical guarantees. Fail-fast batch semantics; legacy/v1/batchcontinuation preserved in standalone mode. - D4 (Capacity & Freshness): Lease-driven discrete freshness states without unsupported millisecond SLOs. Heartbeats renew lease but do not bypass catch-up. Proposed 64 MiB default import limit, 1 concurrent import permit.
- D5 (Import & Publication): Offline candidate build with atomic pointer swap. Create-only vs. mandatory composite percent-encoded precondition (
If-Match) for replacement. Post-build mutex recheck of ownership and preconditions. Strict error segregation: composite precondition mismatch returns412 Precondition Failed(precondition_failed), whereas ownership, fencing, and state conflicts return409 Conflict(graph_already_initialized,fenced_feeder_session,lease_expired). Readers never observe partial state. - D6 (Trust Boundary): Partitioned query vs feeder surfaces. Optional feeder token (
KNOTREL_FEEDER_TOKEN). Reverse proxy required for untrusted networks; M8 brought forward if public exposure is required.
Consequences & Compatibility
- v0.1.0 Backward Compatibility: Preserved completely in standalone mode. Existing Rust crate APIs, REST contracts, batch semantics, and arbitrary-precision decimal revisions remain unchanged.
- Zero Durability Promises: The system explicitly documents that in-memory acceptance does not guarantee persistence in the authoritative source or recovery after restart.
- Implementation Isolation: Features 0.2.2–0.2.6 can implement import, metadata exposure, ordered streaming, and readiness probes against this candidate specification once approved by the maintainer.
Deterministic Testing and Handoff
The contract specifies 34 deterministic traces (TR-01 through TR-34) covering happy paths, stale instance IDs, duplicate events, transient out-of-order sequences, worker timeouts with controlled interleavings, replacement during catch-up, fenced feeders, epoch changes, history expiration, process restart, verification-only barriers, progress-event retries, lease expiry vs synchronization proof, session takeovers during build, predecessor mismatches, hash collisions with 256-byte cursors, partial batch global sequences, declared gaps, equal barrier sequences with mismatching positions (TR-15), older superseded barriers (TR-24), rejected barriers preserving server states (TR-25), uncommitted event retry vs committed duplicate conflict (TR-26), prohibition of progress on failed required mutations (TR-27), import precondition failures vs ownership fencing (TR-28), exact barrier matches while uninitialized (TR-29), barriers during initial baseline loading (TR-30), barriers during replacement loading (TR-31), retained committed sequences with different event IDs (TR-32), retained event IDs reused at new sequences (TR-33), and evicted history boundaries (TR-34).
Feature 0.2.2 is handed an exact candidate interface definition for bounded atomic HTTP import (POST /v1/feeder/import) with preconditions, limits, and atomic publication invariants.