Documentation
¶
Overview ¶
Package session holds per-session protocol state for the MCP kernel: the bounded outstanding-request table, response correlation, cancellation with ownership/direction rules, completion, and expiration/cleanup.
The load-bearing invariant is direction isolation (MCP-PROTO-015 / #925): all correlation state is keyed by (session, direction, request-id), the same JSON-RPC id may be outstanding in BOTH directions at once, and no operation in one direction may resolve, cancel, complete, overwrite, release or delete the other direction's state. A second, closely-related guard (MCP-PROTO-013): an outstanding-request entry is released ONLY on trustworthy same-session correlation — never on an id lifted from a message the kernel just rejected — so a hostile peer cannot delete a legitimate in-flight request by naming its id.
Index ¶
- type CancelResult
- type Clock
- type CorrelateResult
- type Manager
- func (m *Manager) Close(id string)
- func (m *Manager) Get(id string) (*Session, bool)
- func (m *Manager) Open(id string, capability protocol.Capability, role protocol.PeerRole) (*Session, error)
- func (m *Manager) OutstandingFor(capability protocol.Capability) int
- func (m *Manager) SessionCount() int
- func (m *Manager) Sweep() int
- func (m *Manager) TotalOutstanding() int
- type Session
- func (s *Session) Admit(dir protocol.Direction, class jsonrpc.Class, method string) (protocol.Admission, error)
- func (s *Session) Cancel(dir protocol.Direction, owner string, id jsonrpc.ID) CancelResult
- func (s *Session) Capability() protocol.Capability
- func (s *Session) CorrelateResponse(dir protocol.Direction, id jsonrpc.ID) (CorrelateResult, error)
- func (s *Session) ID() string
- func (s *Session) PendingCount() int
- func (s *Session) RegisterRequest(dir protocol.Direction, owner string, id jsonrpc.ID, method string) error
- func (s *Session) Role() protocol.PeerRole
- func (s *Session) SetNegotiatedVersion(v protocol.Version) error
- func (s *Session) State() protocol.State
- func (s *Session) Version() (protocol.Version, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CancelResult ¶
type CancelResult int
CancelResult is the outcome of a cancellation.
const ( // CancelApplied — a pending request owned by the canceller was released. CancelApplied CancelResult = iota + 1 // CancelLate — the target was already resolved (completed or cancelled) — // tolerated no-op, no state changed. CancelLate // CancelNoTarget — no request with this id in this direction — tolerated no-op, // all state retained. CancelNoTarget // CancelRejectedOppositeDirection — the id is outstanding ONLY in the other // direction — rejected, the other direction's entry retained. CancelRejectedOppositeDirection // CancelRejectedWrongOwner — the id is pending in this direction but owned by a // different requestor — rejected, retained. CancelRejectedWrongOwner // CancelRejectedInitialize — the target is the initialize request, which must // not be cancelled — rejected, retained. CancelRejectedInitialize )
func (CancelResult) Applied ¶
func (r CancelResult) Applied() bool
Applied reports whether the cancellation released a request.
type Clock ¶
Clock returns the current time. It is injected so tests are deterministic and so the kernel never reads a wall clock implicitly.
type CorrelateResult ¶
type CorrelateResult int
CorrelateResult is the outcome of correlating an inbound response.
const ( // Completed — the response matched a pending request, which is now released. Completed CorrelateResult = iota + 1 // ToleratedAfterCancel — the response arrived after the request was cancelled; // tolerated per the cancellation spec, not a fault. ToleratedAfterCancel )
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager owns all live sessions and the per-capability outstanding-request budget. It bounds the session count and, INDEPENDENTLY per capability, the total outstanding requests across sessions, and drives expiration. Every mutating method is safe for concurrent use.
Lock discipline (deadlock-free by construction): the outstanding counters are atomic, so the request hot paths (RegisterRequest / CorrelateResponse / Cancel) hold only the session lock and never acquire the manager lock. The manager lock is acquired only by session-directory operations (Open / Close / Sweep / Get / counts), which may then take a session lock — a single, consistent lock order (manager → session), so no ABBA cycle is possible.
func NewManager ¶
NewManager builds a Manager with independent Gateway and Management limit sets and an injected clock. A nil clock defaults to time.Now.
func (*Manager) Close ¶
Close terminates a session and releases its outstanding budget back to the per-capability total. Idempotent.
func (*Manager) Open ¶
func (m *Manager) Open(id string, capability protocol.Capability, role protocol.PeerRole) (*Session, error)
Open creates a new session bound to a capability and peer role. It fails if the id is already open or the Manager is at its (capability-specific) session cap.
func (*Manager) OutstandingFor ¶
func (m *Manager) OutstandingFor(capability protocol.Capability) int
OutstandingFor returns the fleet-wide pending count for one capability.
func (*Manager) SessionCount ¶
SessionCount returns the number of live sessions.
func (*Manager) Sweep ¶
Sweep expires sessions idle for longer than their SessionTTL, and — on sessions that are still active — expires individual outstanding requests older than the same TTL so a peer that keeps a session busy (e.g. pinging) while withholding responses cannot pin the outstanding budget indefinitely. It is a bounded scan over live sessions (no per-entry timer goroutine) and returns the number of SESSIONS reclaimed. Callers invoke it on a cadence; it uses the injected clock.
func (*Manager) TotalOutstanding ¶
TotalOutstanding returns the fleet-wide pending-request count across both capabilities.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is one MCP protocol session. All fields are guarded by mu; callers use the methods, never the fields.
func (*Session) Admit ¶
func (s *Session) Admit(dir protocol.Direction, class jsonrpc.Class, method string) (protocol.Admission, error)
Admit gates a methodful message (request or notification) through the lifecycle rules (bootstrap + one-time handshake) and the reviewed method registry — including the wire-class check (a notification-only method must not carry an id and vice versa) — then advances the lifecycle state on an accepted handshake method. Responses are NOT admitted here — they are correlated (CorrelateResponse).
func (*Session) Cancel ¶
Cancel applies a cancellation naming a request id, owned by owner, in direction dir. It enforces same-direction + owning-requestor + initialize-exempt rules and NEVER deletes state it is not entitled to: an opposite-direction id, a wrong-owner id, and the initialize request are all left intact. A late cancellation (target already resolved) and an unknown id are tolerated no-ops.
func (*Session) Capability ¶
func (s *Session) Capability() protocol.Capability
Capability returns the session's surface.
func (*Session) CorrelateResponse ¶
CorrelateResponse matches an inbound response to a pending request in the SAME direction and releases it. It NEVER touches the other direction, and it releases ONLY on an exact same-direction pending match — an uncorrelated response deletes nothing (the remote-state-deletion guard, MCP-PROTO-013). A second completion of an already-completed request is a duplicate-completion fault; a response after a cancellation is tolerated.
func (*Session) PendingCount ¶
PendingCount returns the number of outstanding requests across both directions.
func (*Session) RegisterRequest ¶
func (s *Session) RegisterRequest(dir protocol.Direction, owner string, id jsonrpc.ID, method string) error
RegisterRequest records an in-flight request in the (direction) outstanding table so its response or cancellation can be correlated. It enforces per-requestor id-uniqueness (a duplicate id already outstanding in this direction is rejected) and the per-session + fleet outstanding bounds. It does NOT interact with the other direction.
func (*Session) SetNegotiatedVersion ¶
SetNegotiatedVersion records the version chosen at initialize. It fails if a version is already negotiated (negotiation happens once) or the version is not supported (default deny — MCP-PROTO-010).