Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:2giosangmitom Sqmeow nvim Session Registration

From Leeroopedia
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:

  1. Registration: insert_connection stores an Arc<Connection> keyed by id, replacing any with the same id.
  2. Call lifecycle: begin_call registers a CancellationToken; cancel trips it; the guard removes the entry on drop.
  3. History eviction: store_run stores results as one group; the oldest run is evicted when the ring buffer is full. Evicted Arc handles remain valid for existing readers.
  4. View generation: begin_view increments a generation counter; finish_view only 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

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment