Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
session_scoped.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
41#pragma once
42
43#include <cstddef>
44#include <mutex>
45#include <string>
46#include <unordered_map>
47
48namespace entropic {
49
60template <typename T>
62public:
71 template <typename Fn>
72 auto with(const std::string& key, Fn&& fn) {
73 std::lock_guard<std::mutex> lock(mutex_);
74 return fn(by_session_[key]);
75 }
76
89 template <typename Fn>
90 auto peek(const std::string& key, Fn&& fn) const {
91 std::lock_guard<std::mutex> lock(mutex_);
92 auto it = by_session_.find(key);
93 return fn(it == by_session_.end() ? nullptr : &it->second);
94 }
95
103 bool release(const std::string& key) {
104 std::lock_guard<std::mutex> lock(mutex_);
105 return by_session_.erase(key) > 0;
106 }
107
114 std::size_t session_count() const {
115 std::lock_guard<std::mutex> lock(mutex_);
116 return by_session_.size();
117 }
118
119private:
120 mutable std::mutex mutex_;
121 std::unordered_map<std::string, T> by_session_;
122};
123
134public:
139 virtual ~SessionStateOwner() = default;
140
148 virtual bool release_session(const std::string& key) = 0;
149
156 virtual std::size_t session_count() const = 0;
157};
158
159} // namespace entropic
One T per session key, under one leaf mutex (gh#158).
auto peek(const std::string &key, Fn &&fn) const
Run fn on key's state WITHOUT creating it.
bool release(const std::string &key)
Forget key's state.
auto with(const std::string &key, Fn &&fn)
Run fn on key's state, creating it empty on first use.
std::size_t session_count() const
Sessions currently holding state (bounded-growth probe).
A server that keeps state per session and can release it (gh#158).
virtual bool release_session(const std::string &key)=0
Drop everything this server holds for key.
virtual ~SessionStateOwner()=default
Virtual destructor for a polymorphic mixin.
virtual std::size_t session_count() const =0
Sessions this server currently holds state for.
Activate model on GPU (WARM → ACTIVE).
@ optional
optional: every escape asks the host's path approver