Principle:2giosangmitom Sqmeow nvim Session Registration
| Knowledge Sources | |
|---|---|
| Domains | Session_Management, Connection_Management |
| Last Updated | 2026-10-16 14:00 GMT |
Overview
A session management principle that registers open connections and running calls in shared state, enabling asynchronous work to outlive the RPC handlers that started them.
Description
Session Registration is the principle of maintaining editor session state — connections, result history, running calls, and view generations — in thread-safe shared containers that live beyond individual RPC requests. Connections and completed results are shared through Arc so async work can safely use handles after the handler returns. Mutex-protected collections own session membership; cloned handles remain valid after eviction or disconnect but are no longer discoverable by id.
This principle addresses the architecture of a long-lived engine serving a short-lived request loop: work started by one RPC call (query execution, edit application) must continue and report results even after the call's handler has returned. The reply is queued before the work starts so the editor can record the accepted id before handling completion events.
Key invariants:
- Connection ids come from Lua; call ids are allocated by the engine (monotonic).
- Running calls are tracked separately from retained history so cancellation does not depend on the result cache.
- A multi-statement run counts as one history slot; all its calls are evicted together.
Usage
Use this principle when designing an RPC server that spawns async work: register work in shared state before spawning, use cancellation tokens for cooperative cancellation, and share results through Arc to allow safe concurrent access after eviction.
Theoretical Basis
The principle rests on shared state with lifecycle management:
- Registration:
insert_connectionstores anArc<Connection>keyed by id, replacing any with the same id. - Call lifecycle:
begin_callregisters aCancellationToken;canceltrips it; the guard removes the entry on drop. - History eviction:
store_runstores results as one group; the oldest run is evicted when the ring buffer is full. EvictedArchandles remain valid for existing readers. - View generation:
begin_viewincrements a generation counter;finish_viewonly commits if the generation is still current, discarding stale results.
# Abstract pattern (NOT real implementation):
class Session:
connections: Mutex[dict[ConnId, Arc[Connection]]]
running: Mutex[dict[CallId, CancellationToken]]
def begin_call(self, id: CallId) -> RunningCall:
token = CancellationToken()
self.running.lock()[id] = token
return RunningCall(self, id, token) # removes on drop
def store_run(self, calls: list[Call]) -> list[Arc[Call]]:
arc_calls = [Arc(c) for c in calls]
with self.calls.lock() as history:
history.order.append([c.id for c in arc_calls])
for c in arc_calls: history.calls[c.id] = c
while len(history.order) > self.limit:
evict_oldest(history) # Arc handles stay valid for readers
return arc_calls