1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Permission rendezvous: ask → permission.asked event → await reply via
//! POST /permission/{id}/reply (oc-remote contract: reply = once|always|reject).
//! Bounded wait (300s) → deny (fail-closed, never hang a session).
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
/// How long an unanswered ask blocks the tool (then deny).
pub const ASK_TIMEOUT: Duration = Duration::from_secs(300);
/// An "always"-granted rule (upstream `Permission.Rule`): a wildcard pattern
/// per permission family. A grant on `bash` with pattern `git status *` covers
/// every future `git status …` — unlike ocserve's old exact-string keys.
#[derive(Clone, Debug, PartialEq)]
pub struct GrantRule {
pub permission: String,
pub pattern: String,
}
#[derive(Default)]
pub struct PermissionGate {
pending: parking_lot::Mutex<HashMap<String, tokio::sync::oneshot::Sender<String>>>,
/// session → granted rules ("always" replies), evaluated with wildcards
always: parking_lot::Mutex<HashMap<String, Vec<GrantRule>>>,
/// sessions whose persisted grants were loaded (one DB read per prompt,
/// not per ask — K-ALWAYS persistence)
hydrated: parking_lot::Mutex<std::collections::HashSet<String>>,
/// snapshots for GET /permission
requests: parking_lot::Mutex<HashMap<String, Value>>,
}
/// Drop-guard: mirrors question::PendingGuard (happy-path-only cleanup is a
/// banned class — AGENTS §2.3/§2.5; antagonism A2).
pub struct PendingGuard {
gate: Arc<PermissionGate>,
id: String,
}
impl Drop for PendingGuard {
fn drop(&mut self) {
self.gate.pending.lock().remove(&self.id);
self.gate.requests.lock().remove(&self.id);
}
}
impl PermissionGate {
pub fn new() -> Arc<Self> {
Arc::new(Self::default())
}
/// Register a pending ask; returns the receiver AND a drop-guard. The
/// guard removes both pending+requests entries on ANY exit (abort/panic
/// included) — timeout cleanup alone misses the killed-task case
/// (antagonism A2; QuestionGate's PendingGuard is the same pattern).
pub fn register(
self: Arc<Self>,
id: &str,
request: Value,
) -> (tokio::sync::oneshot::Receiver<String>, PendingGuard) {
let (tx, rx) = tokio::sync::oneshot::channel();
self.pending.lock().insert(id.to_string(), tx);
self.requests.lock().insert(id.to_string(), request);
let guard = PendingGuard {
gate: self,
id: id.to_string(),
};
(rx, guard)
}
/// Register a pending ask that is NOT tied to a handler frame — the entry
/// lives until `reply`/timeout (the v2 permission oracle's `ask`, whose
/// request must survive the create handler returning). No waiter exists, so
/// the receiver is dropped; `reply` keys on the entry's PRESENCE, not on a
/// successful oneshot send, so this still resolves cleanly.
///
/// Bounded (RAM audit 2026-10-11): unlike `register`, no PendingGuard can
/// ever clean these up, so a client retry loop minting asks would grow
/// `pending`/`requests` without limit. Cap the persistent set — evicting
/// the oldest by request timestamp — and count evictions on a metric.
pub fn register_persistent(&self, id: &str, request: Value) {
const PERSISTENT_CAP: usize = 512;
let (tx, rx) = tokio::sync::oneshot::channel();
{
// lock order: pending -> requests (every other site acquires in
// this order, and none hold both simultaneously — nesting here
// cannot invert).
let mut pending = self.pending.lock();
let mut requests = self.requests.lock();
if requests.len() >= PERSISTENT_CAP {
// evict oldest by request time (millis), fall back to key order
let oldest = requests
.iter()
.min_by_key(|(_, v)| {
v.get("time")
.and_then(|t| t.get("created").or_else(|| t.as_i64().map(|_| t)))
.and_then(|t| t.as_i64())
.unwrap_or(i64::MAX)
})
.map(|(k, _)| k.clone());
if let Some(k) = oldest {
pending.remove(&k);
requests.remove(&k);
ocserve_metrics::counter("ocserve_permission_persistent_evicted_total", 1);
}
}
pending.insert(id.to_string(), tx);
requests.insert(id.to_string(), request);
}
drop(rx);
}
/// K-ALWAYS: load the session's persisted always-grants into memory
/// (once per session per process; prompt lock serializes prompts so the
/// read-then-insert cannot race itself).
pub fn hydrate(&self, db: &std::path::Path, session_id: &str) -> anyhow::Result<()> {
if self.hydrated.lock().contains(session_id) {
return Ok(());
}
let keys = ocserve_store::session_always_keys(db, session_id)?;
if !keys.is_empty() {
let mut always = self.always.lock();
let entry = always.entry(session_id.to_string()).or_default();
for k in keys {
// new form: "<permission>\t<pattern>"; legacy: "<permission>:<pattern>"
let (permission, pattern) = match k.split_once('\t') {
Some((p, pat)) => (p.to_string(), pat.to_string()),
None => match k.split_once(':') {
Some((p, pat)) => (p.to_string(), pat.to_string()),
None => continue,
},
};
let rule = GrantRule {
permission,
pattern,
};
if !entry.contains(&rule) {
entry.push(rule);
}
}
}
self.hydrated.lock().insert(session_id.to_string());
Ok(())
}
/// Gauge support: number of asks awaiting a reply.
pub fn pending_len(&self) -> usize {
self.pending.lock().len()
}
/// Drop all per-session state (RAM audit 2026-10-11: `always`/`hydrated`
/// grew per session and never shrank — a thousands-of-sessions vector).
/// Called on session delete; a re-created session id re-hydrates from the
/// DB on its next prompt.
pub fn forget_session(&self, session_id: &str) {
self.always.lock().remove(session_id);
self.hydrated.lock().remove(session_id);
}
/// Resolve a pending ask from a client reply. Returns true when the id was
/// pending (the request existed), regardless of whether a waiter was
/// listening — the v2 oracle registers asks with no waiter.
///
/// Cascade (upstream permission/index.ts `reply`): a `reject` fails every
/// OTHER pending ask in the same session; an `always` auto-approves any
/// pending ask whose patterns its new grants now cover. The reply string
/// is forwarded verbatim so waiters see "once"/"always"/"reject".
pub fn reply(&self, id: &str, reply: &str) -> bool {
let request = self.requests.lock().remove(id);
let Some(tx) = self.pending.lock().remove(id) else {
return false;
};
let _ = tx.send(reply.to_string());
let session_id = request
.as_ref()
.and_then(|r| r.get("sessionID"))
.and_then(|v| v.as_str())
.map(str::to_string);
// Cascade only for the terminal replies.
if reply == "reject"
&& let Some(sid) = &session_id
{
let others: Vec<String> = {
let reqs = self.requests.lock();
reqs.iter()
.filter(|(k, v)| {
*k != id
&& v.get("sessionID").and_then(|x| x.as_str()) == Some(sid.as_str())
})
.map(|(k, _)| k.clone())
.collect()
};
for oid in others {
self.requests.lock().remove(&oid);
if let Some(otx) = self.pending.lock().remove(&oid) {
let _ = otx.send("reject".to_string());
}
}
} else if reply == "always"
&& let Some(sid) = &session_id
{
// upstream reply pushes the request's `always` patterns as grants
// BEFORE cascading, so the cascade (and future asks) see them.
if let Some(req) = &request {
let permission = req
.get("permission")
.or_else(|| req.get("action"))
.and_then(|v| v.as_str())
.unwrap_or("");
if let Some(always) = req.get("always").and_then(|v| v.as_array()) {
for pat in always.iter().filter_map(|p| p.as_str()) {
self.grant_always(sid, permission, pat);
}
}
}
// re-check other pending asks against this session's grants and
// auto-resolve any now covered (upstream's loop).
let granted = self.granted(sid);
let covered: Vec<String> = {
let reqs = self.requests.lock();
reqs.iter()
.filter(|(k, v)| {
*k != id
&& v.get("sessionID").and_then(|x| x.as_str()) == Some(sid.as_str())
&& patterns_covered(v, &granted)
})
.map(|(k, _)| k.clone())
.collect()
};
for oid in covered {
self.requests.lock().remove(&oid);
if let Some(otx) = self.pending.lock().remove(&oid) {
let _ = otx.send("always".to_string());
}
}
}
true
}
/// Await the reply with timeout; timeout → "reject" (fail closed) AND
/// cleanup (a stale pending entry would 404 every later reply — M2b finding).
pub async fn wait(&self, id: &str, rx: tokio::sync::oneshot::Receiver<String>) -> String {
match tokio::time::timeout(ASK_TIMEOUT, rx).await {
Ok(Ok(reply)) => reply,
_ => {
self.requests.lock().remove(id);
self.pending.lock().remove(id);
"reject".to_string()
}
}
}
/// "always" grant: remember a rule for the session. The store may pass the
/// legacy `"<permission>:<pattern>"` or the new `"<permission>\t<pattern>"`
/// form; both are parsed into a wildcard rule (upstream stores rules, not
/// exact strings, so `git status *` covers `git status --short`).
pub fn grant_always(&self, session_id: &str, permission: &str, pattern: &str) {
let rule = GrantRule {
permission: permission.to_string(),
pattern: pattern.to_string(),
};
let mut always = self.always.lock();
let entry = always.entry(session_id.to_string()).or_default();
if !entry.contains(&rule) {
entry.push(rule);
}
}
/// Whether a granted rule covers this (permission, resource): the rule's
/// pattern matches the resource with wildcards (upstream evaluate over
/// `approved`).
pub fn check_always(&self, session_id: &str, permission: &str, resource: &str) -> bool {
self.always
.lock()
.get(session_id)
.map(|rules| {
rules.iter().any(|r| {
ocserve_tools::wildcard_match(permission, &r.permission)
&& ocserve_tools::wildcard_match(resource, &r.pattern)
})
})
.unwrap_or(false)
}
/// Snapshot of granted rules for a session (used by the reply cascade to
/// re-evaluate sibling pending asks, upstream `reply` "always" branch).
pub fn granted(&self, session_id: &str) -> Vec<GrantRule> {
self.always
.lock()
.get(session_id)
.cloned()
.unwrap_or_default()
}
/// Pending request list for GET /permission.
pub fn list(&self) -> Vec<Value> {
self.requests.lock().values().cloned().collect()
}
}
/// Whether every pattern of a pending request is covered by the granted rules
/// (upstream `item.info.patterns.every(evaluate(...) === "allow")`).
fn patterns_covered(request: &Value, granted: &[GrantRule]) -> bool {
let permission = request
.get("permission")
.or_else(|| request.get("action"))
.and_then(|v| v.as_str())
.unwrap_or("");
let Some(patterns) = request.get("patterns").and_then(|v| v.as_array()) else {
return false;
};
!patterns.is_empty()
&& patterns.iter().all(|p| {
p.as_str().is_some_and(|pat| {
granted.iter().any(|r| {
ocserve_tools::wildcard_match(permission, &r.permission)
&& ocserve_tools::wildcard_match(pat, &r.pattern)
})
})
})
}
#[cfg(test)]
mod guard_tests {
use super::*;
#[test]
fn dropping_pending_guard_clears_entries() {
let gate = Arc::new(PermissionGate::default());
let (rx, guard) = gate
.clone()
.register("perm_t", serde_json::json!({"tool":"bash"}));
assert_eq!(gate.pending_len(), 1);
drop(guard); // abort path: the ask future dies without reply/timeout
assert_eq!(gate.pending_len(), 0, "guard must clear pending (A2)");
assert!(!gate.reply("perm_t", "once"), "already cleared");
drop(rx);
}
#[test]
fn reply_then_guard_drop_is_idempotent() {
let gate = Arc::new(PermissionGate::default());
let (rx, guard) = gate.clone().register("perm_r", serde_json::json!({}));
assert!(gate.reply("perm_r", "allow"));
assert_eq!(gate.pending_len(), 0);
drop(guard); // no-op after reply
assert_eq!(gate.pending_len(), 0);
drop(rx);
}
}
#[cfg(test)]
mod persistent_cap_tests {
use super::*;
/// RAM audit 2026-10-11: the persistent set (no drop-guard path) must be
/// bounded. Negative control: removing the eviction block makes len()
/// exceed the cap (red).
#[test]
fn persistent_registrations_are_capped() {
let gate = PermissionGate::new();
for i in 0..700 {
gate.register_persistent(
&format!("per_{i:04}"),
serde_json::json!({"time": {"created": i as i64}}),
);
}
assert!(
gate.pending_len() <= 512,
"persistent pending set must be capped: {}",
gate.pending_len()
);
assert!(
gate.requests.lock().len() <= 512,
"persistent request snapshots must be capped: {}",
gate.requests.lock().len()
);
// the newest ask survives (oldest evicted, not the newest)
assert!(gate.pending.lock().contains_key("per_0699"));
}
}
#[cfg(test)]
mod forget_session_tests {
use super::*;
/// Session-delete must release per-session gate state (RAM audit
/// 2026-10-11). Negative control: removing the `forget_session` body
/// leaves `always`/`hydrated` populated → red.
#[test]
fn forget_session_releases_state() {
let gate = PermissionGate::new();
{
gate.always
.lock()
.entry("ses_x".into())
.or_default()
.push(GrantRule {
permission: "bash".into(),
pattern: "git *".into(),
});
gate.hydrated.lock().insert("ses_x".into());
}
gate.forget_session("ses_x");
assert!(gate.always.lock().get("ses_x").is_none());
assert!(!gate.hydrated.lock().contains("ses_x"));
// unrelated sessions unaffected
gate.hydrated.lock().insert("ses_y".into());
gate.forget_session("ses_x");
assert!(gate.hydrated.lock().contains("ses_y"));
}
}