Skip to main content

areev_server/
lib.rs

1//! areev-server — the opt-in thin HTTP surface and
2//! the local test console (`areev ui`).
3//!
4//! Deliberately minimal: std-only HTTP/1.1 on 127.0.0.1, one request per
5//! connection, JSON API + one embedded HTML page. This is a *local
6//! inspection console*, not a service — no auth, binds loopback only.
7
8use std::io::{BufRead, BufReader, Read, Write};
9use std::net::{TcpListener, TcpStream};
10use std::time::Duration;
11
12use areev_cal::{CalExecutor, CalExecutorConfig, CalStoreFacade, AreevFacade};
13use areev_loop_adapter::{now_ms, BorrowedSubstrate};
14use serde_json::{json, Value};
15use areev_loop::{Decision, Engine, RecStatus, RunOptions};
16
17const CONSOLE_HTML: &str = include_str!("console.html");
18
19/// Per-connection read/write timeout — bounds slow-client (slowloris) attacks.
20const READ_TIMEOUT_SECS: u64 = 15;
21/// Hard ceiling on bytes read from one connection (1 MiB body cap + headroom
22/// for the request line and headers). Bounds memory against oversized requests.
23const MAX_CONN_BYTES: u64 = (1 << 20) + (128 << 10);
24/// Cap on total header bytes and header count per request.
25const MAX_HEADER_BYTES: usize = 64 * 1024;
26const MAX_HEADERS: usize = 100;
27/// Total wall-clock deadline for reading + handling one request. A per-read
28/// timeout cannot bound a slow-drip client (each byte resets it), so a watchdog
29/// shuts the socket down at this deadline.
30const REQUEST_DEADLINE_SECS: u64 = 30;
31
32
33/// One accepted connection: plaintext, or TLS when the server was built
34/// with the `tls` feature and configured with a certificate. The raw
35/// socket handle stays available for timeouts and the watchdog shutdown;
36/// all request/response bytes flow through this wrapper. A plaintext
37/// client hitting a TLS listener fails the handshake — there is no
38/// downgrade path by construction.
39enum Conn {
40    Plain(TcpStream),
41    #[cfg(feature = "tls")]
42    Tls(Box<rustls::StreamOwned<rustls::ServerConnection, TcpStream>>),
43}
44
45impl Read for Conn {
46    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
47        match self {
48            Conn::Plain(s) => s.read(buf),
49            #[cfg(feature = "tls")]
50            Conn::Tls(s) => s.read(buf),
51        }
52    }
53}
54
55impl Write for Conn {
56    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
57        match self {
58            Conn::Plain(s) => s.write(buf),
59            #[cfg(feature = "tls")]
60            Conn::Tls(s) => s.write(buf),
61        }
62    }
63    fn flush(&mut self) -> std::io::Result<()> {
64        match self {
65            Conn::Plain(s) => s.flush(),
66            #[cfg(feature = "tls")]
67            Conn::Tls(s) => s.flush(),
68        }
69    }
70}
71
72pub struct UiServer {
73    facade: std::sync::Arc<AreevFacade>,
74    executor: CalExecutor,
75    db_label: String,
76    /// Shared secret required for access when set. In hub mode it guards only
77    /// mutating/sync endpoints (`Bearer`); in console-auth mode (`auth_all`) it
78    /// guards every request via `Bearer` **or** HTTP `Basic` (password = token).
79    token: Option<String>,
80    /// When true, *every* request requires the token — the console page, all
81    /// reads, and all writes — and a `401` carries `WWW-Authenticate: Basic` so
82    /// browsers prompt. Set by [`UiServer::with_auth`]; hub mode leaves it off.
83    auth_all: bool,
84    /// Directory for received segments (areevd hub mode).
85    segment_dir: Option<String>,
86    /// When false (the default), reject any request whose `Host` header is not
87    /// loopback — the standard DNS-rebinding defense for a localhost service.
88    /// Set true only when the operator intentionally serves to other hosts
89    /// (CLI `--allow-remote`), where a non-loopback `Host` is expected.
90    allow_remote: bool,
91    /// Host loop policy (§6.2) applied to the `/api/loop/*` engine — the
92    /// same `loop-policy.json` the CLI takes (`areev ui --policy`). Absent →
93    /// the closed default: nothing auto-applies, nothing is denied. Host
94    /// config, never persisted in the memory file.
95    loop_policy: Option<areev_loop::Policy>,
96    /// Multi-principal mode (`areev ui --auth areev-auth.json`): a credential
97    /// map resolving bearer/Basic tokens to principal names. Rights come
98    /// from the FILE's own grant grains — the map holds no policy and no
99    /// raw secrets. Unauthenticated requests run as `anonymous`
100    /// (read-only unless the file grants more). It changes *who* a request
101    /// is, never *whether* the shared `--token-env` secret is required: when
102    /// both are configured the secret keeps its contract (implied admin, and
103    /// still demanded on the requests it guards).
104    credentials: Option<areev_core::authz::CredentialMap>,
105    /// Whether destructive CAL is permitted, kept alongside the executor so
106    /// every builder can rebuild it without clobbering the others' settings.
107    allow_destructive_ops: bool,
108    /// Native TLS (the recorded dependency exception; `tls` cargo feature).
109    /// None = plaintext — the documented default is a TLS-terminating proxy.
110    #[cfg(feature = "tls")]
111    tls_config: Option<std::sync::Arc<rustls::ServerConfig>>,
112    /// SSO v0 — trusted-header auth (`areev ui --sso-header X-Forwarded-User
113    /// --sso-secret-env VAR`): an authenticating proxy does OIDC/SAML and
114    /// forwards the identity in this header. Honored ONLY when the request
115    /// also carries the proxy shared secret in `x-areev-proxy-secret` —
116    /// otherwise identity headers are attacker-controlled input and are
117    /// ignored entirely. Rights still come from the FILE's grant grains.
118    sso_header: Option<String>,
119    sso_secret: Option<String>,
120}
121
122/// Per-request principal binding. The server handles one request at a
123/// time, so rebinding the shared facade for the request's duration is
124/// race-free; the guard restores the owner default on drop — including on
125/// panic — so a failed handler can never leak a stray principal into the
126/// next request.
127struct RequestBinding<'a> {
128    facade: &'a AreevFacade,
129}
130
131impl<'a> RequestBinding<'a> {
132    fn bind(
133        facade: &'a AreevFacade,
134        map: &areev_core::authz::CredentialMap,
135        bearer: Option<&str>,
136        memory: &str,
137    ) -> Self {
138        // Memory-scoped resolution: a credential whose `memories` list
139        // excludes THIS memory is indistinguishable from an unknown token.
140        let principal =
141            bearer.and_then(|t| map.resolve_for_memory(t, memory).ok().map(str::to_string));
142        match principal {
143            Some(p) => {
144                if facade.bind_principal(&p).is_err() {
145                    // A store failure reading grants fails toward LESS
146                    // privilege, never more.
147                    Self::bind_anonymous(facade);
148                }
149            }
150            None => Self::bind_anonymous(facade),
151        }
152        RequestBinding { facade }
153    }
154
155    /// `anonymous` = a read-only baseline plus whatever the file explicitly
156    /// grants the `anonymous` principal — the formalized version of the old
157    /// token-less read-only console.
158    /// Bind a proxy-proven SSO identity directly (no credential map row
159    /// needed — the IdP said who this is; the FILE says what they may do).
160    fn bind_identity(facade: &'a AreevFacade, principal: &str) -> Self {
161        if facade.bind_principal(principal).is_err() {
162            Self::bind_anonymous(facade);
163        }
164        RequestBinding { facade }
165    }
166
167    fn bind_anonymous(facade: &AreevFacade) {
168        use areev_core::authz::{AuthzSet, Grant, Verb};
169        let mut grants = facade
170            .with_store(|m| m.authz_grants("anonymous"))
171            .unwrap_or_default();
172        grants.push(Grant { verbs: vec![Verb::Read], namespaces: vec!["*".to_string()] });
173        facade.bind(AuthzSet::restricted("anonymous", grants));
174    }
175}
176
177impl Drop for RequestBinding<'_> {
178    fn drop(&mut self) {
179        self.facade
180            .bind(areev_core::authz::AuthzSet::owner("user:console"));
181    }
182}
183
184impl UiServer {
185    pub fn new(facade: AreevFacade, db_label: String) -> Self {
186        UiServer {
187            facade: std::sync::Arc::new(facade),
188            executor: CalExecutor::new(CalExecutorConfig::default())
189                .with_governance(std::sync::Arc::new(areev_loop_adapter::LoopGovernance::new())),
190            db_label,
191            token: None,
192            auth_all: false,
193            segment_dir: None,
194            allow_remote: false,
195            loop_policy: None,
196            credentials: None,
197            allow_destructive_ops: CalExecutorConfig::default().allow_destructive_ops,
198            #[cfg(feature = "tls")]
199            tls_config: None,
200            sso_header: None,
201            sso_secret: None,
202        }
203    }
204
205    /// Enable trusted-header SSO (v0). `header` names the identity header
206    /// the authenticating proxy forwards; `secret` is the proxy's shared
207    /// secret, demanded in `x-areev-proxy-secret` on every SSO request.
208    pub fn with_sso(mut self, header: impl Into<String>, secret: impl Into<String>) -> Self {
209        let header = header.into();
210        let secret = secret.into();
211        // An empty proxy secret would let `x-areev-proxy-secret:` (empty)
212        // prove any identity — refuse loudly at the library boundary too,
213        // not just in the CLI.
214        assert!(
215            !header.trim().is_empty() && !secret.trim().is_empty(),
216            "with_sso requires a non-empty header name and proxy secret"
217        );
218        self.sso_header = Some(header.to_ascii_lowercase());
219        self.sso_secret = Some(secret);
220        self
221    }
222
223    /// Rebuild the executor from the current gate settings. Every builder
224    /// that touches the CAL surface goes through here so `--no-destructive-ops`
225    /// and `--policy` compose instead of clobbering each other's config —
226    /// and the governance host survives every rebuild. (Same shape as
227    /// `areev-mcp`'s; building `CalExecutorConfig::default()` in one builder
228    /// silently re-enabled destruction configured off by another.)
229    fn rebuild_executor(&mut self) {
230        self.executor = CalExecutor::new(CalExecutorConfig {
231            allow_destructive_ops: self.allow_destructive_ops,
232            ..CalExecutorConfig::default()
233        })
234        .with_governance(std::sync::Arc::new(match &self.loop_policy {
235            Some(p) => areev_loop_adapter::LoopGovernance::with_policy(p.clone()),
236            None => areev_loop_adapter::LoopGovernance::new(),
237        }));
238    }
239
240    /// Multi-principal mode: attach the credential map (`areev-auth.json`).
241    /// Tokens resolve to principal names; rights come from the memory
242    /// file's own grant grains; unauthenticated requests run as
243    /// `anonymous`. See `docs/cal-reference.md` §9.
244    pub fn with_credentials(mut self, map: areev_core::authz::CredentialMap) -> Self {
245        self.credentials = Some(map);
246        self
247    }
248
249    /// Attach a host loop policy to the console's loop routes, so a
250    /// console-triggered run honors the same auto-apply grants, denies, and
251    /// severity floors as `areev loop run --policy`.
252    pub fn with_loop_policy(mut self, policy: areev_loop::Policy) -> Self {
253        self.loop_policy = Some(policy);
254        // The CAL surface's governance host honors the same policy.
255        self.rebuild_executor();
256        self
257    }
258
259    /// May this request's session read the raw sync surface? A segment is a
260    /// whole-memory op-log export, so it takes `read` on every namespace —
261    /// a principal granted read on one namespace must not be able to pull
262    /// the file wholesale through `/api/segment`. Owner sessions (every
263    /// non-credential-map deployment) pass unchanged.
264    fn session_may_sync(&self) -> bool {
265        self.facade
266            .authz()
267            .allows(areev_core::authz::Verb::Read, "*")
268    }
269
270    /// The loop engine for a request: builtins + the host policy when one
271    /// was attached.
272    fn engine(&self) -> Engine {
273        match &self.loop_policy {
274            Some(p) => Engine::with_builtins().with_policy(p.clone()),
275            None => Engine::with_builtins(),
276        }
277    }
278
279    /// Accept requests whose `Host` header is non-loopback. Off by default so
280    /// the loopback console is protected against DNS-rebinding reads even when
281    /// unauthenticated; the CLI enables it under `--allow-remote`.
282    pub fn allow_remote(mut self, yes: bool) -> Self {
283        self.allow_remote = yes;
284        self
285    }
286
287    /// Require a shared secret on **every** request (the console page, reads,
288    /// and writes). Browsers authenticate through the native `Basic` prompt
289    /// (any username; password = `token`); scripts may send `Authorization:
290    /// Bearer <token>`. Use this to serve the console to more than a single
291    /// trusted local operator. Pair with a TLS-terminating proxy for non-local
292    /// exposure — the token crosses the wire in the clear otherwise.
293    pub fn with_auth(mut self, token: String) -> Self {
294        // An empty token would authenticate `Authorization: Bearer ` (the
295        // empty credential) as the OWNER — refuse at the library boundary,
296        // not just in the CLI's --token-env validation.
297        assert!(
298            !token.trim().is_empty(),
299            "with_auth requires a non-empty token"
300        );
301        self.token = Some(token);
302        self.auth_all = true;
303        self
304    }
305
306    /// Permit or forbid destructive CAL (`FORGET <hash>`) from the query
307    /// console. Enabled by default; pass `false` to serve a read-only console.
308    /// Since the plain console is unauthenticated, disabling this is the safe
309    /// choice when the console is exposed beyond a trusted local operator.
310    pub fn allow_destructive_ops(mut self, allow: bool) -> Self {
311        self.allow_destructive_ops = allow;
312        self.rebuild_executor();
313        self
314    }
315
316    /// areevd mode: require `Authorization: Bearer <token>` on POST
317    /// endpoints and accept segment push/pull under `dir`.
318    pub fn into_hub(mut self, token: Option<String>, dir: &str) -> std::io::Result<Self> {
319        std::fs::create_dir_all(dir)?;
320        self.token = token;
321        self.segment_dir = Some(dir.to_string());
322        // The hub is a network service (segment push/pull from other nodes), so
323        // non-loopback Hosts are expected — writes stay bearer-gated.
324        self.allow_remote = true;
325        Ok(self)
326    }
327
328    /// Bind and return the listener (lets callers learn the ephemeral port).
329    pub fn bind(addr: &str) -> std::io::Result<TcpListener> {
330        TcpListener::bind(addr)
331    }
332
333    /// Serve forever on an already-bound listener.
334    pub fn serve(&self, listener: TcpListener) -> std::io::Result<()> {
335        for stream in listener.incoming() {
336            match stream {
337                Ok(s) => {
338                    if let Err(e) = self.handle(s) {
339                        eprintln!("areev ui: request error: {e}");
340                    }
341                }
342                Err(e) => eprintln!("areev ui: accept error: {e}"),
343            }
344        }
345        Ok(())
346    }
347
348    /// Configure native TLS from PEM files (`tls` cargo feature). The
349    /// documented default remains a TLS-terminating proxy; this is for
350    /// deployments with nowhere to put one (edge boxes, appliances).
351    #[cfg(feature = "tls")]
352    pub fn with_tls(mut self, cert_pem: &str, key_pem: &str) -> std::io::Result<Self> {
353        use rustls::pki_types::pem::PemObject;
354        let pem_err =
355            |e: rustls::pki_types::pem::Error| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string());
356        let certs: Vec<rustls::pki_types::CertificateDer<'static>> =
357            rustls::pki_types::CertificateDer::pem_file_iter(cert_pem)
358                .map_err(pem_err)?
359                .collect::<Result<_, _>>()
360                .map_err(pem_err)?;
361        let key =
362            rustls::pki_types::PrivateKeyDer::from_pem_file(key_pem).map_err(pem_err)?;
363        let config = rustls::ServerConfig::builder()
364            .with_no_client_auth()
365            .with_single_cert(certs, key)
366            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
367        self.tls_config = Some(std::sync::Arc::new(config));
368        Ok(self)
369    }
370
371    /// Wrap an accepted socket per the TLS posture. The handshake happens
372    /// lazily on first read/write; a plaintext client fails it loudly.
373    fn wrap(&self, stream: TcpStream) -> std::io::Result<Conn> {
374        #[cfg(feature = "tls")]
375        if let Some(config) = &self.tls_config {
376            let session = rustls::ServerConnection::new(std::sync::Arc::clone(config))
377                .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
378            return Ok(Conn::Tls(Box::new(rustls::StreamOwned::new(session, stream))));
379        }
380        Ok(Conn::Plain(stream))
381    }
382
383    fn handle(&self, stream: TcpStream) -> std::io::Result<()> {
384        // The server handles one connection at a time, so a single slow-drip
385        // client could otherwise hold it open indefinitely (a per-read timeout
386        // resets on every byte). A watchdog thread enforces a hard wall-clock
387        // deadline by shutting the socket down; it is unparked and joined the
388        // instant the request completes, so it adds no latency in the fast path.
389        let watchdog_stream = stream.try_clone()?;
390        let done = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
391        let watchdog_done = std::sync::Arc::clone(&done);
392        let watchdog = std::thread::spawn(move || {
393            std::thread::park_timeout(Duration::from_secs(REQUEST_DEADLINE_SECS));
394            if !watchdog_done.load(std::sync::atomic::Ordering::Acquire) {
395                let _ = watchdog_stream.shutdown(std::net::Shutdown::Both);
396            }
397        });
398        let raw = stream.try_clone()?;
399        let conn = self.wrap(stream)?;
400        let result = self.handle_request(&raw, conn);
401        done.store(true, std::sync::atomic::Ordering::Release);
402        watchdog.thread().unpark();
403        let _ = watchdog.join();
404        result
405    }
406
407    fn handle_request(&self, raw: &TcpStream, mut conn: Conn) -> std::io::Result<()> {
408        // Bound slow clients (slowloris) and total bytes read from the
409        // connection so a malicious client cannot stall the server or exhaust
410        // memory with an oversized request line / headers / body. The
411        // timeouts sit on the RAW socket, so they bound TLS handshakes too.
412        raw.set_read_timeout(Some(Duration::from_secs(READ_TIMEOUT_SECS)))?;
413        raw.set_write_timeout(Some(Duration::from_secs(READ_TIMEOUT_SECS)))?;
414        let mut reader = BufReader::new((&mut conn).take(MAX_CONN_BYTES));
415        let mut request_line = String::new();
416        reader.read_line(&mut request_line)?;
417        let mut parts = request_line.split_whitespace();
418        let method = parts.next().unwrap_or("").to_string();
419        let path = parts.next().unwrap_or("/").to_string();
420
421        // headers → content-length + authorization + origin + host
422        let mut content_length = 0usize;
423        let mut bearer: Option<String> = None;
424        let mut origin: Option<String> = None;
425        let mut host: Option<String> = None;
426        let mut sso_identity_raw: Option<String> = None;
427        let mut proxy_secret: Option<String> = None;
428        let mut header_bytes = 0usize;
429        let mut header_count = 0usize;
430        loop {
431            let mut line = String::new();
432            let n = reader.read_line(&mut line)?;
433            if n == 0 {
434                break; // EOF / connection closed by peer
435            }
436            header_bytes += n;
437            header_count += 1;
438            if header_bytes > MAX_HEADER_BYTES || header_count > MAX_HEADERS {
439                return Err(std::io::Error::new(
440                    std::io::ErrorKind::InvalidData,
441                    "request headers too large",
442                ));
443            }
444            let l = line.trim();
445            if l.is_empty() {
446                break;
447            }
448            let low = l.to_ascii_lowercase();
449            if let Some(v) = low.strip_prefix("content-length:") {
450                content_length = v.trim().parse().unwrap_or(0);
451            }
452            if let Some(v) = low.strip_prefix("origin:") {
453                origin = Some(v.trim().to_string());
454            }
455            if let Some(v) = low.strip_prefix("host:") {
456                host = Some(v.trim().to_string());
457            }
458            if let Some(v) = low.strip_prefix("x-areev-proxy-secret:") {
459                // Secrets are compared, never logged; the lowercased copy is
460                // wrong for comparison, so re-slice the original line.
461                let _ = v;
462                if let Some((_, orig)) = l.split_once(':') {
463                    proxy_secret = Some(orig.trim().to_string());
464                }
465            }
466            if let Some(h) = &self.sso_header {
467                if low.starts_with(&format!("{h}:")) {
468                    if let Some((_, orig)) = l.split_once(':') {
469                        sso_identity_raw = Some(orig.trim().to_string());
470                    }
471                }
472            }
473            if low.starts_with("authorization:") {
474                if let Some((_, v)) = l.split_once(':') {
475                    let v = v.trim();
476                    // Accept `Bearer <token>` (scripts/CLI) or HTTP `Basic`
477                    // (browsers, via the native login prompt). For Basic the
478                    // credential is base64(user:pass) and the token is the
479                    // password; the username is ignored.
480                    bearer = if let Some(t) =
481                        v.strip_prefix("Bearer ").or_else(|| v.strip_prefix("bearer "))
482                    {
483                        Some(t.trim().to_string())
484                    } else if let Some(b64) =
485                        v.strip_prefix("Basic ").or_else(|| v.strip_prefix("basic "))
486                    {
487                        basic_auth_password(b64.trim())
488                    } else {
489                        Some(v.to_string())
490                    };
491                }
492            }
493        }
494        // DNS-rebinding defense: a browser tricked into pointing an attacker
495        // domain at 127.0.0.1 sends that domain in the Host header. Unless the
496        // operator opted into remote serving, reject any non-loopback Host on
497        // *every* method (the Origin check below only covers POST, so this is
498        // what stops a drive-by page from reading memory over GET). A missing
499        // Host (bare HTTP/1.0, some CLI clients) passes, mirroring Origin.
500        if !self.allow_remote && host.as_deref().is_some_and(|h| !host_is_local(h)) {
501            let payload = br#"{"ok":false,"error":"non-loopback Host rejected (use --allow-remote to serve remotely)"}"#;
502            drop(reader);
503            let mut out = conn;
504            write!(
505                out,
506                "HTTP/1.1 403 Forbidden\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
507                payload.len()
508            )?;
509            out.write_all(payload)?;
510            return out.flush();
511        }
512
513        let mut body = vec![0u8; content_length.min(1 << 20)];
514        if content_length > 0 {
515            reader.read_exact(&mut body)?;
516        }
517
518        // Cross-origin drive-by protection: browsers attach an Origin header
519        // to cross-site requests. Only loopback pages (the console itself)
520        // may mutate; curl/CLI clients send no Origin and pass through.
521        if method == "POST" && origin.as_deref().is_some_and(|o| !origin_is_local(o)) {
522            let payload = br#"{"ok":false,"error":"cross-origin request rejected"}"#;
523            drop(reader);
524            let mut out = conn;
525            write!(
526                out,
527                "HTTP/1.1 403 Forbidden\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
528                payload.len()
529            )?;
530            out.write_all(payload)?;
531            return out.flush();
532        }
533
534        // The identity header is TRUSTED only when the proxy proved itself
535        // (constant-time secret check). A forged header without the secret
536        // is silently ignored — the request proceeds as whatever its other
537        // credentials make it.
538        let sso_identity: Option<String> = match (&self.sso_secret, proxy_secret, sso_identity_raw) {
539            (Some(expected), Some(presented), Some(id))
540                if ct_eq(expected.as_bytes(), presented.as_bytes())
541                    && !id.trim().is_empty() =>
542            {
543                Some(id)
544            }
545            _ => None,
546        };
547        let (status, ctype, payload) =
548            self.route(&method, &path, &body, bearer.as_deref(), sso_identity.as_deref());
549        // On a console-auth 401, challenge with Basic so browsers show the
550        // native login prompt (any username; password = token).
551        let auth_challenge = if self.auth_all && status.starts_with("401") {
552            "WWW-Authenticate: Basic realm=\"Areev console\", charset=\"UTF-8\"\r\n"
553        } else {
554            ""
555        };
556        drop(reader);
557        let mut out = conn;
558        write!(
559            out,
560            "HTTP/1.1 {status}\r\nContent-Type: {ctype}\r\n{auth_challenge}Content-Length: {}\r\nConnection: close\r\n\r\n",
561            payload.len()
562        )?;
563        out.write_all(&payload)?;
564        out.flush()
565    }
566
567    /// The runtime driver for the /api/run/* surface. The console never
568    /// EXECUTES tools (list/inspect read; respond/cancel journal) — the
569    /// executor is a refusing stub, present only to satisfy the driver
570    /// shape.
571    fn runner(&self, principal: &str) -> areev_run::Runner {
572        struct NoExec;
573        impl areev_run::HostToolExecutor for NoExec {
574            fn execute(
575                &self,
576                tool_name: &str,
577                _h: &str,
578                _i: &Value,
579                _k: &str,
580            ) -> areev_run::ExecResult {
581                areev_run::ExecResult::Err {
582                    cause: areev_run::FailCause::ExecutorError,
583                    detail: format!("the console does not execute tools ('{tool_name}')"),
584                }
585            }
586        }
587        areev_run::Runner {
588            facade: std::sync::Arc::clone(&self.facade),
589            clock: std::sync::Arc::new(areev_run::SystemClock),
590            executor: std::sync::Arc::new(NoExec),
591            llm: None,
592            observer: None,
593            ns: self
594                .facade
595                .default_namespace()
596                .unwrap_or("shared")
597                .to_string(),
598            principal: principal.to_string(),
599        }
600    }
601
602    fn route(
603        &self,
604        method: &str,
605        path: &str,
606        body: &[u8],
607        bearer: Option<&str>,
608        sso_identity: Option<&str>,
609    ) -> (&'static str, &'static str, Vec<u8>) {
610        // Auth: console-auth mode (`auth_all`) guards every request; hub mode
611        // guards only mutating + sync endpoints. The credential arrives as a
612        // Bearer token or an HTTP Basic password (see `handle_request`).
613        //
614        // A credential map changes WHO a request is, not WHETHER the shared
615        // secret is required. When both are configured, a map token
616        // authenticates as its principal, the shared secret authenticates as
617        // the implied admin (`docs/cal-all-you-need-proposal.md` §5.2b), and
618        // anything else is refused — an attached map must never silently
619        // downgrade a `--token-env` console to an open `anonymous` one.
620        let mut shared_secret_ok = false;
621        if let Some(tok) = &self.token {
622            let guarded = self.auth_all || method == "POST" || path.starts_with("/api/segment");
623            shared_secret_ok = bearer.is_some_and(|b| ct_eq(b.as_bytes(), tok.as_bytes()));
624            let known = shared_secret_ok
625                || sso_identity.is_some()
626                || match (&self.credentials, bearer) {
627                    (Some(map), Some(b)) => map.resolve_for_memory(b, &self.db_label).is_ok(),
628                    _ => false,
629                };
630            if guarded && !known {
631                return ("401 Unauthorized", "application/json",
632                        br#"{"ok":false,"error":"authentication required"}"#.to_vec());
633            }
634        } else if self.credentials.is_none() && sso_identity.is_none() && method == "POST" {
635            // §5.7: token-less `areev ui` is read-only. The ONLY POST allowed is
636            // a read-only CAL statement; every write (any loop mutation, an
637            // ADD/SUPERSEDE/FORGET CAL batch, etc.) requires --token-env. This
638            // closes the bypass where a local process could execute a
639            // proposal's CAL directly and skip the review queue.
640            let base = path.split_once('?').map(|(p, _)| p).unwrap_or(path);
641            let allowed = base == "/api/cal" && cal_body_is_read_only(body);
642            if !allowed {
643                return ("401 Unauthorized", "application/json",
644                        br#"{"ok":false,"error":"read-only console: restart areev ui with --token-env VAR to enable writes"}"#.to_vec());
645            }
646        }
647
648        // Credential-map mode: resolve this request's principal and bind
649        // the facade for the request's duration (the guard restores the
650        // owner default on drop). Every verb check downstream — CAL,
651        // loop scopes, the run gate below — now answers for THIS caller.
652        // A request authenticated by the shared secret keeps the owner
653        // session that secret has always carried.
654        let _session = match (&self.credentials, shared_secret_ok) {
655            (Some(map), false) => Some(RequestBinding::bind(&self.facade, map, bearer, &self.db_label)),
656            _ => None,
657        };
658        // The request's resolved PRINCIPAL identity — Some only when a
659        // per-principal credential authenticated it. The shared secret and
660        // anonymous access deliberately resolve to None: approvals need an
661        // identity, and "whoever holds the console token" is not one.
662        let mut request_principal: Option<String> = match (&self.credentials, shared_secret_ok, bearer)
663        {
664            (Some(map), false, Some(b)) => map.resolve_for_memory(b, &self.db_label).ok().map(str::to_string),
665            _ => None,
666        };
667        // SSO v0: a proxy-proven identity becomes the request principal when
668        // no bearer credential resolved one (machines keep tokens; SSO is
669        // for humans behind the proxy). Rights come from the file's grants.
670        let _sso_session = match (&request_principal, sso_identity, shared_secret_ok) {
671            (None, Some(identity), false) => {
672                request_principal = Some(identity.to_string());
673                Some(RequestBinding::bind_identity(&self.facade, identity))
674            }
675            _ => None,
676        };
677
678        let (path, query) = match path.split_once('?') {
679            Some((p, q)) => (p, q),
680            None => (path, ""),
681        };
682        let q = |key: &str| -> Option<String> {
683            query.split('&').find_map(|kv| {
684                let (k, v) = kv.split_once('=')?;
685                (k == key).then(|| urldecode(v))
686            })
687        };
688        match (method, path) {
689            ("GET", "/") => (
690                "200 OK",
691                "text/html; charset=utf-8",
692                CONSOLE_HTML
693                    .replace("{{DB}}", &html_escape(&self.db_label))
694                    .into_bytes(),
695            ),
696            ("POST", "/api/cal") => {
697                let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
698                let queryt = req.get("query").and_then(|v| v.as_str()).unwrap_or("");
699                match self.executor.execute(queryt, &*self.facade) {
700                    Ok(res) => ok_json(json!({
701                        "ok": true,
702                        "result": res.result,
703                        "warnings": res.warnings,
704                        "statement": res.metadata.statement_type,
705                        "elapsed_ms": res.metadata.execution_time_ms,
706                    })),
707                    Err(e) => {
708                        // Structured error: code + span + hint let the console
709                        // point at the offending token instead of just quoting.
710                        let mut err = json!({
711                            "ok": false,
712                            "error": e.sanitize_for_client(),
713                            "code": e.code(),
714                        });
715                        if let Some(sp) = e.span() {
716                            err["span"] = json!({
717                                "start": sp.start, "end": sp.end,
718                                "line": sp.line, "col": sp.col,
719                            });
720                        }
721                        if let Some(hint) = e.suggestion() {
722                            err["suggestion"] = json!(hint);
723                        }
724                        ok_json(err)
725                    }
726                }
727            }
728            ("GET", "/api/stats") => {
729                let stats = self.facade.with_store(|m| m.stats());
730                match stats {
731                    Ok(s) => ok_json(json!({
732                        "db": self.db_label,
733                        "grains": s.grains, "current": s.current,
734                        "triples": s.triples, "terms": s.terms,
735                        "ops": s.ops, "events_indexed": s.events_indexed,
736                    })),
737                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
738                }
739            }
740            ("GET", "/api/log") => {
741                let since: i64 = q("since").and_then(|v| v.parse().ok()).unwrap_or(0);
742                let limit: usize = q("limit").and_then(|v| v.parse().ok()).unwrap_or(100);
743                match self.facade.with_store(|m| m.changes_since(since, limit)) {
744                    Ok(ops) => {
745                        let rows: Vec<Value> = ops
746                            .iter()
747                            .map(|o| {
748                                json!({
749                                    "op_seq": o.op_seq, "hlc": o.hlc,
750                                    "op": match o.op { 1 => "add", 2 => "supersede", 3 => "forget", _ => "?" },
751                                    "hash": o.hash.to_hex(),
752                                })
753                            })
754                            .collect();
755                        ok_json(json!(rows))
756                    }
757                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
758                }
759            }
760            // Who am I: the principal this request is bound to. In
761            // credential-map mode this reflects the per-request binding;
762            // read-only and mode-agnostic.
763            ("GET", "/api/whoami") => {
764                let a = self.facade.authz();
765                ok_json(json!({
766                    "ok": true,
767                    "principal": a.principal(),
768                    "owner": a.is_owner(),
769                    "mode": if self.credentials.is_some() { "credentials" }
770                            else if self.token.is_some() { "token" }
771                            else { "open" },
772                }))
773            }
774            ("GET", "/api/config") => {
775                // Read-only observability: the *effective* per-process
776                // configuration. Nothing here is persisted in the .db —
777                // the file holds data only; these values are supplied by
778                // the host at open time.
779                let cfg = self.executor.config();
780                let (index_text, embedder_dim, declared_embed, mut warnings) =
781                    self.facade.with_store(|m| {
782                        (
783                            m.index_text_enabled(),
784                            m.embedder_dim(),
785                            m.declared_embedding().map(|(mm, d)| (mm.to_string(), d)),
786                            m.open_warnings().to_vec(),
787                        )
788                    });
789                if let Some((m, d)) = &declared_embed {
790                    if embedder_dim.is_none() {
791                        warnings.push(format!(
792                            "vector leg dormant: file expects {m}@{d}, no embedding backend installed"
793                        ));
794                    }
795                }
796                // Saved queries and templates the file carries that this build
797                // cannot load. Same class of thing as an open warning — the
798                // file and the host disagree — so it belongs in the same list.
799                warnings.extend(self.facade.meta_warnings());
800                // Anonymization observability (REQ-ANON-5: no policy is
801                // loud): declared per-ns modes, the host floor, and the
802                // audit counters. Modes only — never mappings or values.
803                let (anon_declared, anon_floor, anon_audit) = self.facade.with_store(|m| {
804                    (
805                        m.anon_declared(),
806                        m.anonymize_egress_floor(),
807                        m.anon_audit_counts(),
808                    )
809                });
810                let anonymization = json!({
811                    "policies": anon_declared
812                        .iter()
813                        .map(|(ns, mode)| json!({"ns": ns, "mode": mode}))
814                        .collect::<Vec<_>>(),
815                    "floor": anon_floor,
816                    "audit_counts": anon_audit
817                        .iter()
818                        .map(|(ns, cat, n)| json!({"ns": ns, "category": cat, "count": n}))
819                        .collect::<Vec<_>>(),
820                });
821                ok_json(json!({
822                    "ok": true,
823                    "db": self.db_label,
824                    "warnings": warnings,
825                    "anonymization": anonymization,
826                    "file": {
827                        "text_index": index_text,
828                        "embedding": declared_embed.map(|(m, d)| json!({"model": m, "dim": d})),
829                    },
830                    "session": {
831                        "namespace": self.facade.session_namespace(),
832                        "mounts": self.facade.mount_aliases(),
833                        // Gates the console's "All namespaces" picker option —
834                        // same rule /api/browse?ns=* and /api/namespaces enforce.
835                        "is_owner": self.facade.authz().is_owner(),
836                    },
837                    "store": {
838                        "index_text": index_text,
839                        "embedder": embedder_dim.map(|d| json!({"dim": d})),
840                    },
841                    "recall": {
842                        "fusion": "rrf",
843                        "rrf_k0": areev_store::RRF_K0,
844                        "overfetch_factor": AreevFacade::RECALL_OVERFETCH,
845                        "legs": {
846                            "structural": true,
847                            "bm25": index_text,
848                            "vector": embedder_dim.is_some(),
849                        },
850                    },
851                    "executor": {
852                        "max_limit": cfg.max_limit,
853                        "default_limit": cfg.default_limit,
854                        "tier1_writes": cfg.tier1_enabled,
855                        "namespace_override": cfg.namespace_override,
856                        "user_id_override": cfg.user_id_override,
857                    },
858                    "server": {
859                        "hub_mode": self.segment_dir.is_some(),
860                        "auth_required": self.token.is_some(),
861                        // true = every request is authenticated (console-auth);
862                        // false with auth_required = hub mode (writes/sync only).
863                        "auth_all": self.auth_all,
864                        "segment_dir": self.segment_dir,
865                    },
866                    "persistence": "per-process (host-supplied at open) — not stored in the .db",
867                }))
868            }
869            ("GET", "/api/browse") => {
870                // Browse-without-queries: the tail of the op-log joined with
871                // grain summaries, newest first. Supersession and tombstone
872                // status are resolved within the returned window so the
873                // console can dim/strike them (grains are never mutated —
874                // this reads the index + immutable blobs only).
875                let limit: i64 = q("limit")
876                    .and_then(|v| v.parse().ok())
877                    .unwrap_or(500)
878                    .clamp(1, 2000);
879                let built = self.facade.with_store(|m| -> Result<(i64, Vec<Value>), String> {
880                    let total = m.stats().map_err(|e| e.to_string())?.ops as i64;
881                    let after = (total - limit).max(0);
882                    let ops = m.changes_since(after, limit as usize).map_err(|e| e.to_string())?;
883                    let mut rows: Vec<Value> = Vec::with_capacity(ops.len());
884                    let mut idx: std::collections::HashMap<String, usize> =
885                        std::collections::HashMap::new();
886                    for o in &ops {
887                        let hex = o.hash.to_hex();
888                        let op_name = match o.op { 1 => "add", 2 => "supersede", 3 => "forget", _ => "?" };
889                        if o.op == 3 {
890                            // tombstone: flag the earlier row if it is in the
891                            // window, else emit a stub (blob is already gone).
892                            match idx.get(&hex) {
893                                Some(&i) => { rows[i]["forgotten"] = json!(true); }
894                                None => rows.push(json!({
895                                    "hash": hex, "op_seq": o.op_seq, "hlc": o.hlc,
896                                    "op": op_name, "forgotten": true,
897                                })),
898                            }
899                            continue;
900                        }
901                        // A SUPERSEDE logs two ops for the new grain (add +
902                        // supersede) — merge them into one row, keeping the
903                        // later op label.
904                        if let Some(&i) = idx.get(&hex) {
905                            rows[i]["op"] = json!(op_name);
906                            rows[i]["op_seq"] = json!(o.op_seq);
907                            rows[i]["hlc"] = json!(o.hlc);
908                            continue;
909                        }
910                        let mut row = json!({
911                            "hash": hex, "op_seq": o.op_seq, "hlc": o.hlc, "op": op_name,
912                        });
913                        match m.get(&o.hash) {
914                            Ok(g) => {
915                                row["type"] = json!(format!("{:?}", g.grain_type).to_lowercase());
916                                row["fields"] = serde_json::to_value(&g.fields).unwrap_or(Value::Null);
917                            }
918                            // erased since (forget op outside the window)
919                            Err(_) => { row["missing"] = json!(true); }
920                        }
921                        idx.insert(hex, rows.len());
922                        rows.push(row);
923                    }
924                    // A supersede op's grain points at its predecessor via
925                    // derived_from; mark the predecessor if it is in-window.
926                    let marks: Vec<(String, String)> = rows.iter()
927                        .filter(|r| r["op"] == "supersede")
928                        .filter_map(|r| {
929                            let old = r["fields"].get("derived_from")?.as_str()?;
930                            Some((old.to_string(), r["hash"].as_str()?.to_string()))
931                        })
932                        .collect();
933                    for (old, newer) in marks {
934                        if let Some(&i) = idx.get(&old) {
935                            rows[i]["superseded_by"] = json!(newer);
936                        }
937                    }
938                    rows.reverse();
939                    Ok((total, rows))
940                });
941                // Scope the browse to a namespace, the way CAL already scopes
942                // recall. Without this the rail lists entities (`john`, `bob`)
943                // that every query on this console then reports as missing,
944                // because the two disagree about what is in view.
945                //
946                // `?ns=<name>` lets the console browse a namespace other than
947                // the one it was launched with, and `?ns=*` lifts the filter
948                // entirely — both read-gated against the session's own
949                // `AuthzSet` exactly as a `RECALL ... WHERE namespace = ...`
950                // would be (`changes_since` itself reads the whole op-log
951                // unscoped; this filter is the only enforcement point, so it
952                // has to check what `AreevFacade::recall` checks, not less).
953                // Omitting `?ns` keeps the original session-default behavior
954                // byte-for-byte. Rows we cannot attribute — tombstone stubs,
955                // grains erased since — are kept regardless: dropping them
956                // would hide an erasure.
957                let ns_param = q("ns");
958                let built = built.and_then(|(total, rows)| {
959                    let authz = self.facade.authz();
960                    let filter_ns = match ns_param.as_deref() {
961                        Some("*") => {
962                            if !authz.is_owner() {
963                                return Err("not authorized to browse all namespaces".to_string());
964                            }
965                            None
966                        }
967                        Some(ns) => {
968                            authz
969                                .check(areev_core::authz::Verb::Read, ns)
970                                .map_err(|e| e.to_string())?;
971                            Some(ns.to_string())
972                        }
973                        None => self.facade.session_namespace().map(|s| s.to_string()),
974                    };
975                    let Some(ns) = filter_ns else {
976                        return Ok((total, rows));
977                    };
978                    let kept = rows
979                        .into_iter()
980                        .filter(|r| {
981                            r["fields"]
982                                .get("namespace")
983                                .and_then(Value::as_str)
984                                .is_none_or(|got| got == ns)
985                        })
986                        .collect();
987                    Ok((total, kept))
988                });
989                match built {
990                    Ok((total, rows)) => ok_json(json!({"ok": true, "total_ops": total, "grains": rows})),
991                    Err(e) => ok_json(json!({"ok": false, "error": e})),
992                }
993            }
994            ("GET", "/api/namespaces") => {
995                // Every namespace present in the file, for the console's
996                // namespace picker. A restricted principal only ever sees the
997                // ones its own grants cover — read access to the *names* of
998                // namespaces it cannot read from would itself be a disclosure.
999                let result = self.facade.with_store(|m| m.namespaces());
1000                match result {
1001                    Ok(list) => {
1002                        let authz = self.facade.authz();
1003                        let owner = authz.is_owner();
1004                        let arr: Vec<Value> = list
1005                            .into_iter()
1006                            .filter(|(ns, _)| {
1007                                owner || authz.check(areev_core::authz::Verb::Read, ns).is_ok()
1008                            })
1009                            .map(|(ns, n)| json!({"namespace": ns, "count": n}))
1010                            .collect();
1011                        ok_json(json!({"ok": true, "namespaces": arr}))
1012                    }
1013                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1014                }
1015            }
1016            ("GET", "/api/grain") => {
1017                let hash = q("hash").unwrap_or_default();
1018                match areev_core::error::Hash::from_hex(&hash)
1019                    .and_then(|h| self.facade.get(&h))
1020                {
1021                    Ok(g) => ok_json(json!({
1022                        "hash": g.hash.to_hex(),
1023                        "type": format!("{:?}", g.grain_type).to_lowercase(),
1024                        "fields": g.fields,
1025                    })),
1026                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1027                }
1028            }
1029            ("GET", "/api/anon/preview") => {
1030                // "As the model sees it" (proposal §8.3): the grain rendered
1031                // the way an egress boundary would show it, policy or not.
1032                let hash = q("hash").unwrap_or_default();
1033                match areev_core::error::Hash::from_hex(&hash)
1034                    .and_then(|h| self.facade.with_store(|m| m.anon_preview(&h)))
1035                {
1036                    Ok(g) => ok_json(json!({
1037                        "hash": g.hash.to_hex(),
1038                        "type": format!("{:?}", g.grain_type).to_lowercase(),
1039                        "fields": g.fields,
1040                    })),
1041                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1042                }
1043            }
1044            ("GET", "/api/verify") => match self.facade.with_store(|m| m.verify()) {
1045                Ok(r) => ok_json(json!({
1046                    "integrity": r.integrity, "grains": r.grains,
1047                    "hash_mismatches": r.hash_mismatches, "undecodable": r.undecodable,
1048                })),
1049                Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1050            },
1051            ("POST", "/api/segment") => {
1052                // A pushed segment is imported straight into the live store,
1053                // so it is a write — and one that never crosses the facade's
1054                // verb checks. Gate it on the bound session's `write`, or a
1055                // principal with no write grant could add grains here that
1056                // `POST /api/cal` refuses it.
1057                if let Err(e) = self
1058                    .facade
1059                    .authz()
1060                    .check(areev_core::authz::Verb::Write, "*")
1061                {
1062                    return ("403 Forbidden", "application/json",
1063                            json!({"ok": false, "error": e.to_string()}).to_string().into_bytes());
1064                }
1065                // push: body = raw MGB1 bundle; applied immediately + archived
1066                let Some(dir) = &self.segment_dir else {
1067                    return ("400 Bad Request", "application/json",
1068                            br#"{"ok":false,"error":"not in hub mode"}"#.to_vec());
1069                };
1070                let name = q("name").unwrap_or_else(|| format!("push-{}.mgb", now_label()));
1071                let Some(safe) = safe_segment_name(&name) else {
1072                    return ("400 Bad Request", "application/json",
1073                            br#"{"ok":false,"error":"invalid segment name"}"#.to_vec());
1074                };
1075                let path = format!("{dir}/{safe}");
1076                if std::fs::write(&path, body).is_err() {
1077                    return ("500 Internal Server Error", "application/json",
1078                            br#"{"ok":false,"error":"write failed"}"#.to_vec());
1079                }
1080                match self.facade.with_store(|m| m.import_bundle(&path)) {
1081                    Ok(st) => ok_json(json!({"ok": true, "applied": st.applied, "skipped": st.skipped, "stored": safe})),
1082                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1083                }
1084            }
1085            ("GET", "/api/segments") | ("GET", "/api/segment") if !self.session_may_sync() => (
1086                "403 Forbidden",
1087                "application/json",
1088                br#"{"ok":false,"error":"AUT-E001: this session lacks read on the whole memory"}"#.to_vec(),
1089            ),
1090            ("GET", "/api/segments") => {
1091                let Some(dir) = &self.segment_dir else {
1092                    return ok_json(json!([]));
1093                };
1094                let mut names: Vec<String> = std::fs::read_dir(dir)
1095                    .map(|d| d.filter_map(|e| e.ok().map(|e| e.file_name().to_string_lossy().to_string()))
1096                        .filter(|n| n.ends_with(".mgb")).collect())
1097                    .unwrap_or_default();
1098                names.sort();
1099                ok_json(json!(names))
1100            }
1101            ("GET", "/api/segment") => {
1102                let Some(dir) = &self.segment_dir else {
1103                    return ("400 Bad Request", "application/json", b"{}".to_vec());
1104                };
1105                let name = q("name").unwrap_or_default();
1106                let Some(safe) = safe_segment_name(&name) else {
1107                    return ("400 Bad Request", "application/json",
1108                            br#"{"ok":false,"error":"invalid segment name"}"#.to_vec());
1109                };
1110                match std::fs::read(format!("{dir}/{safe}")) {
1111                    Ok(b) => ("200 OK", "application/octet-stream", b),
1112                    Err(_) => ("404 Not Found", "application/json",
1113                               br#"{"ok":false,"error":"no such segment"}"#.to_vec()),
1114                }
1115            }
1116            // ── Areev Loop API (§5.4) — GETs are reads (token-less OK); the POST
1117            //    mutations are guarded above (token-less → 401). ────────────
1118            // ── /api/run/*: the governed runtime's console surface ──
1119            ("GET", "/api/run/list") => {
1120                let runner = self.runner("console:read");
1121                match runner.recent_runs(50) {
1122                    Ok(ids) => {
1123                        let rows: Vec<Value> = ids
1124                            .iter()
1125                            .map(|id| {
1126                                let outcome = self.facade.with_store(|m| {
1127                                    m.recent(
1128                                        "agent:harness",
1129                                        Some(areev_core::types::GrainType::Observation),
1130                                        4096,
1131                                    )
1132                                })
1133                                .ok()
1134                                .and_then(|rows| {
1135                                    rows.into_iter().find(|g| {
1136                                        g.get_str("run_id") == Some(id.as_str())
1137                                            && g.get_str("observation_kind")
1138                                                == Some("run_outcome")
1139                                    })
1140                                });
1141                                serde_json::json!({
1142                                    "run_id": id,
1143                                    "outcome": outcome.as_ref().and_then(|g| g.get_str("object")).unwrap_or("open"),
1144                                })
1145                            })
1146                            .collect();
1147                        ok_json(serde_json::json!({"ok": true, "runs": rows}))
1148                    }
1149                    Err(e) => run_api_err(&e.to_string()),
1150                }
1151            }
1152            ("GET", "/api/run/inspect") => {
1153                let Some(run_id) = q("run_id") else {
1154                    return run_api_err("run_id required");
1155                };
1156                // Same Runner::inspect the CLI and bindings call (issue
1157                // #34) — this used to hand-roll a divergent, smaller subset
1158                // (no pinned/budgets/fork_of/superstep/phase).
1159                match self.runner("console:read").inspect(&run_id) {
1160                    Ok(report) => {
1161                        let mut v = serde_json::to_value(&report).unwrap_or(Value::Null);
1162                        if let Some(o) = v.as_object_mut() {
1163                            o.insert("ok".into(), Value::Bool(true));
1164                        }
1165                        ok_json(v)
1166                    }
1167                    Err(e) => run_api_err(&e.to_string()),
1168                }
1169            }
1170            ("POST", "/api/run/respond") => {
1171                // [R3] SHARED-TOKEN APPROVALS ARE REFUSED: an approval whose
1172                // "identity" is a console-wide secret voids the
1173                // approver-identity edge of the provenance chain. Only a
1174                // per-principal credential (areev ui --auth) may respond.
1175                let Some(responder) = request_principal.clone() else {
1176                    return ("403 Forbidden", "application/json",
1177                        br#"{"ok":false,"error":"run.respond requires a per-principal credential (areev ui --auth <map>); shared-token or anonymous approvals are refused - the approver's identity IS the audit record"}"#.to_vec());
1178                };
1179                let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
1180                let (Some(run_id), Some(ask)) = (
1181                    req.get("run_id").and_then(Value::as_str),
1182                    req.get("tool_call_id").and_then(Value::as_str),
1183                ) else {
1184                    return run_api_err("run_id and tool_call_id required");
1185                };
1186                let result = req.get("result").cloned().unwrap_or(Value::Null);
1187                let is_error = req.get("is_error").and_then(Value::as_bool).unwrap_or(false);
1188                let runner = self.runner(&responder);
1189                match runner.respond(run_id, ask, result, is_error, &responder) {
1190                    Ok(()) => ok_json(serde_json::json!({
1191                        "ok": true, "responded": ask, "responder": responder,
1192                        "note": "resume the run to spend the compute",
1193                    })),
1194                    Err(e) => run_api_err(&e.to_string()),
1195                }
1196            }
1197            ("POST", "/api/run/cancel") => {
1198                // The kill switch keeps its LOW bar: any authenticated POST
1199                // may brake a run (the identity label still records who).
1200                let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
1201                let Some(run_id) = req.get("run_id").and_then(Value::as_str) else {
1202                    return run_api_err("run_id required");
1203                };
1204                let because = req.get("because").and_then(Value::as_str).unwrap_or("canceled via console");
1205                let who = request_principal.clone().unwrap_or_else(|| "console:shared".into());
1206                let runner = self.runner(&who);
1207                match runner.cancel(run_id, &who, because) {
1208                    Ok(()) => ok_json(serde_json::json!({"ok": true, "canceled": run_id})),
1209                    Err(e) => run_api_err(&e.to_string()),
1210                }
1211            }
1212            ("GET", "/api/loop/recommendations") => {
1213                let status = q("status").and_then(|s| status_from_str(&s));
1214                let sub = BorrowedSubstrate::new(&self.facade);
1215                match self.engine().recommendations(&sub, status) {
1216                    Ok(recs) => ok_json(json!({
1217                        "ok": true,
1218                        "recommendations": recs.iter().map(rec_json).collect::<Vec<_>>(),
1219                    })),
1220                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
1221                }
1222            }
1223            ("GET", "/api/loop/health") => {
1224                let sub = BorrowedSubstrate::new(&self.facade);
1225                match self.engine().health(&sub, now_ms()) {
1226                    Ok(h) => ok_json(json!({"ok": true, "health": h})),
1227                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
1228                }
1229            }
1230            ("GET", "/api/loop/outcomes") => {
1231                let sub = BorrowedSubstrate::new(&self.facade);
1232                match self.engine().outcomes(&sub) {
1233                    Ok(o) => ok_json(json!({"ok": true, "outcomes": o})),
1234                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
1235                }
1236            }
1237            ("GET", "/api/loop/analyzers") => {
1238                // Effective settings (manifest merged with the file-config), so
1239                // the Setup view renders accurate on/off state and floors.
1240                let sub = BorrowedSubstrate::new(&self.facade);
1241                match self.engine().analyzer_settings(&sub) {
1242                    Ok(list) => ok_json(json!({"ok": true, "analyzers": list})),
1243                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
1244                }
1245            }
1246            ("GET", "/api/loop/telemetry") => {
1247                // Recall-telemetry rollups for the Sessions view. A read — open
1248                // in read-only mode like the other loop GETs.
1249                let mode = self.facade.with_store(|m| m.telemetry_mode());
1250                if mode == areev_store::TelemetryMode::Off {
1251                    ok_json(json!({"ok": true, "enabled": false}))
1252                } else {
1253                    let access = self.facade.with_store(|m| m.telemetry_access_stats(None));
1254                    let queries = self.facade.with_store(|m| m.telemetry_query_stats(None));
1255                    let budget = self.facade.with_store(|m| m.telemetry_budget_stats());
1256                    match (access, queries, budget) {
1257                        (Ok(mut a), Ok(mut q), Ok(b)) => {
1258                            // Most-recalled first; recurring-gap questions first.
1259                            a.sort_by_key(|x| std::cmp::Reverse(x.recall_count));
1260                            a.truncate(200);
1261                            q.sort_by_key(|x| std::cmp::Reverse(x.run_count));
1262                            q.truncate(200);
1263                            ok_json(json!({
1264                                "ok": true,
1265                                "enabled": true,
1266                                "mode": mode.as_str(),
1267                                "access": a.iter().map(|x| json!({
1268                                    "hash": x.hash, "recall_count": x.recall_count, "last_ms": x.last_ms,
1269                                })).collect::<Vec<_>>(),
1270                                "queries": q.iter().map(|x| json!({
1271                                    "sample": x.sample, "run_count": x.run_count,
1272                                    "empty_count": x.empty_count,
1273                                })).collect::<Vec<_>>(),
1274                                "budget": {
1275                                    "sample_count": b.sample_count,
1276                                    "overflow_count": b.overflow_count,
1277                                },
1278                            }))
1279                        }
1280                        _ => ok_json(json!({"ok": false, "error": "telemetry read failed"})),
1281                    }
1282                }
1283            }
1284            ("POST", "/api/loop/run") => {
1285                let authz = self.facade.authz();
1286                if !authz.is_owner()
1287                    && !authz.allows(areev_core::authz::Verb::LoopRun, areev_loop::LOOP_NS)
1288                {
1289                    return ("403 Forbidden", "application/json",
1290                            br#"{"ok":false,"error":"AUT-E001: this session lacks loop.run"}"#.to_vec());
1291                }
1292                let mut sub = BorrowedSubstrate::new(&self.facade);
1293                // The trigger is recorded as co-creator on LLM/external
1294                // findings, so the same principal cannot later approve them.
1295                // In credential-map mode the actor IS the bound principal —
1296                // a request cannot claim an identity.
1297                let actor = if self.credentials.is_some() {
1298                    authz.principal().to_string()
1299                } else {
1300                    serde_json::from_slice::<Value>(body)
1301                        .ok()
1302                        .and_then(|v| v.get("actor").and_then(Value::as_str).map(str::to_string))
1303                        .unwrap_or_else(|| "user:console".to_string())
1304                };
1305                let opts = RunOptions { triggering_actor: Some(actor), ..Default::default() };
1306                match self.engine().run(&mut sub, &opts, now_ms()) {
1307                    Ok(res) => ok_json(json!({"ok": true, "run": res})),
1308                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
1309                }
1310            }
1311            ("POST", "/api/loop/review") => self.loop_review(body),
1312            ("POST", "/api/loop/apply") => self.loop_apply(body),
1313            ("POST", "/api/loop/rollback") => self.loop_rollback(body),
1314            ("POST", "/api/loop/config") => self.loop_config(body),
1315            ("POST", "/api/anon/config") => self.anon_config(body),
1316            _ => (
1317                "404 Not Found",
1318                "application/json",
1319                br#"{"ok":false,"error":"not found"}"#.to_vec(),
1320            ),
1321        }
1322    }
1323
1324    /// The console session's scopes derive from its grants (an owner
1325    /// session — today's only mode — holds all of them); actor
1326    /// `user:console` unless overridden, observer derived from the actor
1327    /// label, never from request text.
1328    fn loop_review(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
1329        let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
1330        let hash = req.get("hash").and_then(Value::as_str).unwrap_or("");
1331        let because = req.get("because").and_then(Value::as_str).unwrap_or("");
1332        let bound = self.facade.authz();
1333        let actor = if self.credentials.is_some() {
1334            bound.principal().to_string()
1335        } else {
1336            req.get("actor").and_then(Value::as_str).unwrap_or("user:console").to_string()
1337        };
1338        let actor = actor.as_str();
1339        let decision = if req.get("decision").and_then(Value::as_str) == Some("reject") {
1340            Decision::Reject
1341        } else {
1342            Decision::Approve
1343        };
1344        let mut sub = BorrowedSubstrate::new(&self.facade);
1345        let scopes = areev_loop_adapter::scopes_for(&self.facade.authz());
1346        let observer = areev_loop_adapter::observer_for_principal(actor);
1347        match self.engine().review(
1348            &mut sub, hash, decision, actor, observer, &scopes, because, now_ms(),
1349        ) {
1350            Ok(()) => ok_json(json!({"ok": true})),
1351            Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
1352        }
1353    }
1354
1355    fn loop_apply(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
1356        let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
1357        let hash = req.get("hash").and_then(Value::as_str).unwrap_or("");
1358        let because = req.get("because").and_then(Value::as_str).unwrap_or("");
1359        let bound = self.facade.authz();
1360        let actor = if self.credentials.is_some() {
1361            bound.principal().to_string()
1362        } else {
1363            req.get("actor").and_then(Value::as_str).unwrap_or("user:console").to_string()
1364        };
1365        let actor = actor.as_str();
1366        let allow_destructive = req.get("allow_destructive").and_then(Value::as_bool).unwrap_or(false);
1367        let mut sub = BorrowedSubstrate::new(&self.facade);
1368        let scopes = areev_loop_adapter::scopes_for(&self.facade.authz());
1369        let observer = areev_loop_adapter::observer_for_principal(actor);
1370        match self.engine().apply(
1371            &mut sub, hash, actor, observer, &scopes, because, allow_destructive, now_ms(),
1372        ) {
1373            Ok(applied) => ok_json(json!({"ok": true, "rollbackable": applied.rollbackable})),
1374            Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
1375        }
1376    }
1377
1378    fn loop_rollback(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
1379        let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
1380        let hash = req.get("hash").and_then(Value::as_str).unwrap_or("");
1381        let because = req.get("because").and_then(Value::as_str).unwrap_or("");
1382        let bound = self.facade.authz();
1383        let actor = if self.credentials.is_some() {
1384            bound.principal().to_string()
1385        } else {
1386            req.get("actor").and_then(Value::as_str).unwrap_or("user:console").to_string()
1387        };
1388        let actor = actor.as_str();
1389        let mut sub = BorrowedSubstrate::new(&self.facade);
1390        let scopes = areev_loop_adapter::scopes_for(&self.facade.authz());
1391        let observer = areev_loop_adapter::observer_for_principal(actor);
1392        match self.engine().rollback(
1393            &mut sub, hash, actor, observer, &scopes, because, now_ms(),
1394        ) {
1395            Ok(()) => ok_json(json!({"ok": true})),
1396            Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
1397        }
1398    }
1399
1400    /// Edit one analyzer's file-config from the Setup view. The body is
1401    /// `{analyzer_id, enabled?, severity_floor?, clear_floor?, params?,
1402    /// namespaces?}`; absent fields are left unchanged. The console holds all
1403    /// scopes (local root of trust), so `Admin` is satisfied; an unknown
1404    /// analyzer or bad param is a structured `ok:false` (not a 500).
1405    fn loop_config(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
1406        let id = serde_json::from_slice::<Value>(body)
1407            .ok()
1408            .and_then(|v| v.get("analyzer_id").and_then(Value::as_str).map(str::to_string))
1409            .unwrap_or_default();
1410        // The update reads the same body; the extra `analyzer_id` key is ignored.
1411        let update: areev_loop::AnalyzerConfigUpdate = serde_json::from_slice(body).unwrap_or_default();
1412        let mut sub = BorrowedSubstrate::new(&self.facade);
1413        match self.engine().set_analyzer_config(&mut sub, &id, update, &areev_loop_adapter::scopes_for(&self.facade.authz())) {
1414            Ok(cfg) => ok_json(json!({"ok": true, "config": cfg})),
1415            Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
1416        }
1417    }
1418}
1419
1420impl UiServer {
1421    /// Declare or clear a per-namespace anonymization policy from the
1422    /// console (Connect → Settings). Rides the same write gate as every
1423    /// console POST (auth + Origin + body cap upstream); the policy itself
1424    /// validates fail-closed in the store.
1425    fn anon_config(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
1426        let v: Value = match serde_json::from_slice(body) {
1427            Ok(v) => v,
1428            Err(e) => return ok_json(json!({"ok": false, "error": e.to_string()})),
1429        };
1430        let Some(ns) = v.get("ns").and_then(Value::as_str).filter(|n| !n.trim().is_empty())
1431        else {
1432            return ok_json(json!({"ok": false, "error": "ns is required"}));
1433        };
1434        let result = if v.get("clear").and_then(Value::as_bool) == Some(true) {
1435            self.facade.with_store(|m| m.clear_anon_policy(ns))
1436        } else {
1437            match v.get("policy") {
1438                Some(policy) => {
1439                    let policy_json = policy.to_string();
1440                    self.facade.with_store(|m| m.set_anon_policy(ns, &policy_json))
1441                }
1442                None => {
1443                    return ok_json(json!({"ok": false, "error": "policy (object) or clear: true is required"}));
1444                }
1445            }
1446        };
1447        match result {
1448            Ok(()) => {
1449                let declared = self.facade.with_store(|m| m.anon_declared());
1450                ok_json(json!({"ok": true, "policies": declared
1451                    .iter()
1452                    .map(|(ns, mode)| json!({"ns": ns, "mode": mode}))
1453                    .collect::<Vec<_>>()}))
1454            }
1455            Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1456        }
1457    }
1458}
1459
1460/// True when a `host[:port]` (or `[ipv6]:port`) authority names a loopback
1461/// host. Shared by the Origin drive-by check and the Host-header
1462/// (DNS-rebinding) check.
1463fn host_is_local(host_port: &str) -> bool {
1464    let host = if let Some(h) = host_port.strip_prefix('[') {
1465        h.split(']').next().unwrap_or("")
1466    } else {
1467        host_port.split(':').next().unwrap_or("")
1468    };
1469    matches!(host, "127.0.0.1" | "localhost" | "::1")
1470}
1471
1472/// True when an Origin header names a loopback host (any port, http/https).
1473fn origin_is_local(origin: &str) -> bool {
1474    let rest = origin
1475        .strip_prefix("http://")
1476        .or_else(|| origin.strip_prefix("https://"));
1477    let Some(rest) = rest else { return false };
1478    let host_port = rest.split('/').next().unwrap_or("");
1479    host_is_local(host_port)
1480}
1481
1482fn now_label() -> String {
1483    format!("{}", std::time::SystemTime::now()
1484        .duration_since(std::time::UNIX_EPOCH)
1485        .map(|d| d.as_millis())
1486        .unwrap_or(0))
1487}
1488
1489fn ok_json(v: Value) -> (&'static str, &'static str, Vec<u8>) {
1490    ("200 OK", "application/json", v.to_string().into_bytes())
1491}
1492
1493fn run_api_err(msg: &str) -> (&'static str, &'static str, Vec<u8>) {
1494    (
1495        "400 Bad Request",
1496        "application/json",
1497        serde_json::json!({"ok": false, "error": msg}).to_string().into_bytes(),
1498    )
1499}
1500
1501/// True when a `POST /api/cal` body is a read-only statement. Classification
1502/// is delegated to `areev-cal`'s exhaustive [`areev_cal::classify`] module —
1503/// the single source of truth — so grammar growth cannot desynchronize this
1504/// gate (a keyword sniff lived here before and would have silently
1505/// misclassified any new statement). Fail closed: unparseable text is not
1506/// read-only.
1507fn cal_body_is_read_only(body: &[u8]) -> bool {
1508    let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
1509    let q = req.get("query").and_then(Value::as_str).unwrap_or("");
1510    areev_cal::classify::query_is_read_only(q)
1511}
1512
1513fn status_from_str(s: &str) -> Option<RecStatus> {
1514    match s {
1515        "pending" => Some(RecStatus::Pending),
1516        "approved" => Some(RecStatus::Approved),
1517        "rejected" => Some(RecStatus::Rejected),
1518        "applied" => Some(RecStatus::Applied),
1519        "rolled_back" => Some(RecStatus::RolledBack),
1520        "expired" => Some(RecStatus::Expired),
1521        _ => None, // includes "all"
1522    }
1523}
1524
1525fn rec_json(r: &areev_loop::Recommendation) -> Value {
1526    json!({
1527        "hash": r.hash,
1528        "status": r.status.as_str(),
1529        "severity": r.severity.as_str(),
1530        "analyzer": r.analyzer,
1531        "summary": r.summary.render(),
1532        "target_ref": r.target_ref,
1533        "destructive": r.destructive,
1534        "rollbackable": r.rollbackable,
1535        "evidence": r.evidence,
1536    })
1537}
1538
1539fn urldecode(s: &str) -> String {
1540    let mut out = Vec::new();
1541    let b = s.as_bytes();
1542    let mut i = 0;
1543    while i < b.len() {
1544        match b[i] {
1545            b'%' if i + 2 < b.len() + 1 && i + 2 < b.len() + 1 => {
1546                if i + 2 < b.len() + 1 && i + 2 < b.len() {
1547                    let hex = std::str::from_utf8(&b[i + 1..i + 3]).unwrap_or("");
1548                    if let Ok(v) = u8::from_str_radix(hex, 16) {
1549                        out.push(v);
1550                        i += 3;
1551                        continue;
1552                    }
1553                }
1554                out.push(b[i]);
1555                i += 1;
1556            }
1557            b'+' => {
1558                out.push(b' ');
1559                i += 1;
1560            }
1561            c => {
1562                out.push(c);
1563                i += 1;
1564            }
1565        }
1566    }
1567    String::from_utf8_lossy(&out).to_string()
1568}
1569
1570fn html_escape(s: &str) -> String {
1571    s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;")
1572}
1573
1574/// Constant-time byte comparison — avoids leaking the bearer token through
1575/// response timing. A length mismatch fails fast (token length is not secret).
1576fn ct_eq(a: &[u8], b: &[u8]) -> bool {
1577    if a.len() != b.len() {
1578        return false;
1579    }
1580    let mut diff = 0u8;
1581    for (x, y) in a.iter().zip(b.iter()) {
1582        diff |= x ^ y;
1583    }
1584    diff == 0
1585}
1586
1587/// Decode standard base64 (RFC 4648), enough for HTTP Basic credentials.
1588/// Hand-rolled to keep the server dependency-free. Returns `None` on any
1589/// invalid character or truncated input.
1590fn base64_decode(s: &str) -> Option<Vec<u8>> {
1591    fn val(c: u8) -> Option<u32> {
1592        match c {
1593            b'A'..=b'Z' => Some((c - b'A') as u32),
1594            b'a'..=b'z' => Some((c - b'a' + 26) as u32),
1595            b'0'..=b'9' => Some((c - b'0' + 52) as u32),
1596            b'+' => Some(62),
1597            b'/' => Some(63),
1598            _ => None,
1599        }
1600    }
1601    let s = s.trim().trim_end_matches('=');
1602    let mut out = Vec::with_capacity(s.len() * 3 / 4);
1603    let (mut acc, mut bits) = (0u32, 0u32);
1604    for &c in s.as_bytes() {
1605        acc = (acc << 6) | val(c)?;
1606        bits += 6;
1607        if bits >= 8 {
1608            bits -= 8;
1609            out.push((acc >> bits) as u8);
1610        }
1611    }
1612    Some(out)
1613}
1614
1615/// Extract the password from an HTTP Basic credential (`base64("user:pass")`).
1616/// The console ignores the username and treats the password as the token.
1617/// Returns `None` if the value is not valid base64/UTF-8 or has no `:`.
1618fn basic_auth_password(b64: &str) -> Option<String> {
1619    let text = String::from_utf8(base64_decode(b64)?).ok()?;
1620    text.split_once(':').map(|(_user, pass)| pass.to_string())
1621}
1622
1623/// Sanitize a client-supplied segment name into a single safe filename.
1624/// Strips anything outside `[A-Za-z0-9-._]`, then requires the result to be
1625/// exactly one normal path component — rejecting empty names, `.`/`..`, and
1626/// path separators so a push/pull cannot escape the segment directory.
1627fn safe_segment_name(name: &str) -> Option<String> {
1628    let safe: String = name
1629        .chars()
1630        .filter(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '.' | '_'))
1631        .collect();
1632    if safe.is_empty() || safe.len() > 128 {
1633        return None;
1634    }
1635    let mut comps = std::path::Path::new(&safe).components();
1636    match (comps.next(), comps.next()) {
1637        (Some(std::path::Component::Normal(c)), None) if c.to_str() == Some(safe.as_str()) => {
1638            Some(safe)
1639        }
1640        _ => None,
1641    }
1642}
1643
1644#[cfg(test)]
1645mod loop_route_tests {
1646    use super::UiServer;
1647    use areev_cal::AreevFacade;
1648    use areev_core::types::{Fact, Grain};
1649    use areev_store::Areev;
1650
1651    fn server(auth: Option<&str>) -> UiServer {
1652        let dir = tempfile::tempdir().unwrap();
1653        let path = dir.path().join("t.db");
1654        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
1655        // Two identical facts → a duplicate-consolidation recommendation.
1656        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller")).unwrap();
1657        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller")).unwrap();
1658        std::mem::forget(dir); // keep the file alive for the server's lifetime
1659        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
1660        let s = UiServer::new(facade, "test".into());
1661        match auth {
1662            Some(t) => s.with_auth(t.to_string()),
1663            None => s,
1664        }
1665    }
1666
1667    fn text(r: &(&str, &str, Vec<u8>)) -> String {
1668        String::from_utf8_lossy(&r.2).to_string()
1669    }
1670
1671    #[test]
1672    fn token_less_console_is_read_only() {
1673        let s = server(None);
1674        // A loop mutation (run) is a write → 401.
1675        assert!(s.route("POST", "/api/loop/run", b"{}", None, None).0.starts_with("401"));
1676        // A write CAL → 401.
1677        let w = s.route("POST", "/api/cal", br#"{"query":"ADD fact SET subject=\"x\""}"#, None, None);
1678        assert!(w.0.starts_with("401"), "write CAL must be 401: {}", text(&w));
1679        // A read CAL → 200.
1680        let r = s.route("POST", "/api/cal", br#"{"query":"RECALL facts WHERE subject = \"acme\""}"#, None, None);
1681        assert!(r.0.starts_with("200"), "read CAL allowed: {}", text(&r));
1682        // Areev Loop reads stay open token-less.
1683        assert!(s.route("GET", "/api/loop/recommendations", b"", None, None).0.starts_with("200"));
1684    }
1685
1686    #[test]
1687    fn telemetry_endpoint_is_an_open_read() {
1688        let s = server(None);
1689        let r = s.route("GET", "/api/loop/telemetry", b"", None, None);
1690        assert!(r.0.starts_with("200"), "telemetry read open: {}", text(&r));
1691        let body = text(&r);
1692        assert!(body.contains("\"ok\":true"), "{body}");
1693        // The test store opens bare (telemetry off) → enabled:false.
1694        assert!(body.contains("\"enabled\":false"), "{body}");
1695    }
1696
1697    #[test]
1698    fn authenticated_run_review_apply_roundtrip() {
1699        let s = server(Some("tok"));
1700        let run = s.route("POST", "/api/loop/run", b"{}", Some("tok"), None);
1701        assert!(run.0.starts_with("200"), "run: {}", text(&run));
1702        assert!(text(&run).contains("\"ran\""));
1703
1704        let list = s.route("GET", "/api/loop/recommendations?status=pending", b"", Some("tok"), None);
1705        let v: serde_json::Value = serde_json::from_slice(&list.2).unwrap();
1706        let recs = v["recommendations"].as_array().unwrap();
1707        assert!(!recs.is_empty(), "at least one recommendation");
1708        let hash = recs[0]["hash"].as_str().unwrap().to_string();
1709
1710        let rev = s.route(
1711            "POST",
1712            "/api/loop/review",
1713            format!(r#"{{"hash":"{hash}","decision":"approve","because":"ok"}}"#).as_bytes(),
1714            Some("tok"),
1715            None,
1716        );
1717        assert!(text(&rev).contains("\"ok\":true"), "review: {}", text(&rev));
1718
1719        let ap = s.route(
1720            "POST",
1721            "/api/loop/apply",
1722            format!(r#"{{"hash":"{hash}","because":"go"}}"#).as_bytes(),
1723            Some("tok"),
1724            None,
1725        );
1726        assert!(text(&ap).contains("\"ok\":true"), "apply: {}", text(&ap));
1727    }
1728
1729    #[test]
1730    fn config_edit_toggles_analyzer_via_console() {
1731        let s = server(Some("tok"));
1732        // Token-less write → 401 (guarded like every POST).
1733        assert!(s.route("POST", "/api/loop/config", b"{}", None, None).0.starts_with("401"));
1734
1735        // Read the analyzers; pick one that is on by default.
1736        let list = s.route("GET", "/api/loop/analyzers", b"", Some("tok"), None);
1737        let v: serde_json::Value = serde_json::from_slice(&list.2).unwrap();
1738        let id = v["analyzers"]
1739            .as_array()
1740            .unwrap()
1741            .iter()
1742            .find(|x| x["enabled"] == true)
1743            .expect("some analyzer is on")["id"]
1744            .as_str()
1745            .unwrap()
1746            .to_string();
1747
1748        // Disable it via the console endpoint.
1749        let post = s.route(
1750            "POST",
1751            "/api/loop/config",
1752            format!(r#"{{"analyzer_id":"{id}","enabled":false}}"#).as_bytes(),
1753            Some("tok"),
1754            None,
1755        );
1756        assert!(text(&post).contains("\"ok\":true"), "config: {}", text(&post));
1757
1758        // It reads back disabled.
1759        let list2 = s.route("GET", "/api/loop/analyzers", b"", Some("tok"), None);
1760        let v2: serde_json::Value = serde_json::from_slice(&list2.2).unwrap();
1761        let now = v2["analyzers"]
1762            .as_array()
1763            .unwrap()
1764            .iter()
1765            .find(|x| x["id"] == id.as_str())
1766            .unwrap();
1767        assert_eq!(now["enabled"], false, "toggled off and persisted");
1768    }
1769
1770    #[test]
1771    fn self_approval_surfaces_lop_code() {
1772        let s = server(Some("tok"));
1773        s.route("POST", "/api/loop/run", b"{}", Some("tok"), None);
1774        let list = s.route("GET", "/api/loop/recommendations?status=pending", b"", Some("tok"), None);
1775        let v: serde_json::Value = serde_json::from_slice(&list.2).unwrap();
1776        let rec = &v["recommendations"].as_array().unwrap()[0];
1777        let hash = rec["hash"].as_str().unwrap();
1778        let analyzer = rec["analyzer"].as_str().unwrap();
1779        let creator = format!("engine:{analyzer}");
1780        // The engine actor approving its own proposal is blocked.
1781        let rev = s.route(
1782            "POST",
1783            "/api/loop/review",
1784            format!(r#"{{"hash":"{hash}","decision":"approve","because":"self","actor":"{creator}"}}"#).as_bytes(),
1785            Some("tok"),
1786            None,
1787        );
1788        assert!(text(&rev).contains("LOP-E021"), "self-approval blocked: {}", text(&rev));
1789    }
1790}
1791
1792#[cfg(test)]
1793mod security_tests {
1794    use super::{base64_decode, basic_auth_password, ct_eq, safe_segment_name};
1795
1796    #[test]
1797    fn ct_eq_matches_only_equal() {
1798        assert!(ct_eq(b"secret-token", b"secret-token"));
1799        assert!(!ct_eq(b"secret-token", b"secret-toker"));
1800        assert!(!ct_eq(b"secret", b"secret-token")); // length mismatch
1801        assert!(ct_eq(b"", b""));
1802    }
1803
1804    #[test]
1805    fn base64_decode_roundtrips_known_vectors() {
1806        // RFC 4648 test vectors (with and without padding).
1807        assert_eq!(base64_decode("").unwrap(), b"");
1808        assert_eq!(base64_decode("Zg==").unwrap(), b"f");
1809        assert_eq!(base64_decode("Zm8=").unwrap(), b"fo");
1810        assert_eq!(base64_decode("Zm9v").unwrap(), b"foo");
1811        assert_eq!(base64_decode("Zm9vYmFy").unwrap(), b"foobar");
1812        // padding is optional for us
1813        assert_eq!(base64_decode("Zg").unwrap(), b"f");
1814        // invalid characters are rejected
1815        assert!(base64_decode("****").is_none());
1816    }
1817
1818    #[test]
1819    fn basic_auth_extracts_password_ignoring_username() {
1820        // base64("areev:s3cret") and base64(":s3cret") both yield the token.
1821        assert_eq!(basic_auth_password("ZGVqYTpzM2NyZXQ=").as_deref(), Some("s3cret"));
1822        assert_eq!(basic_auth_password("OnMzY3JldA==").as_deref(), Some("s3cret"));
1823        // a password may itself contain ':' — only the first ':' splits.
1824        // base64("u:a:b") -> "a:b"
1825        assert_eq!(basic_auth_password("dTphOmI=").as_deref(), Some("a:b"));
1826        // no ':' at all → not a valid Basic credential
1827        assert_eq!(basic_auth_password("bm9jb2xvbg=="), None); // "nocolon"
1828        assert_eq!(basic_auth_password("****"), None); // not base64
1829    }
1830
1831    #[test]
1832    fn safe_segment_name_blocks_traversal() {
1833        assert_eq!(safe_segment_name("push-123.mgb").as_deref(), Some("push-123.mgb"));
1834        assert_eq!(safe_segment_name(".."), None);
1835        assert_eq!(safe_segment_name("."), None);
1836        assert_eq!(safe_segment_name(""), None);
1837        // Separators are stripped, so any accepted name is a single component
1838        // that cannot escape the segment directory.
1839        for probe in ["../../etc/passwd", "/etc/passwd", "a/../b", "foo/bar"] {
1840            if let Some(s) = safe_segment_name(probe) {
1841                assert!(!s.contains('/') && s != ".." && s != ".", "unsafe: {s}");
1842            }
1843        }
1844    }
1845}
1846
1847#[cfg(test)]
1848mod credential_route_tests {
1849    use super::UiServer;
1850    use areev_cal::AreevFacade;
1851    use areev_core::authz::{CredentialMap, AUTHZ_NS, REL_PERMITS};
1852    use areev_core::types::{Fact, Grain};
1853    use areev_store::Areev;
1854
1855    /// A server in credential-map mode: two env-referenced tokens, rights
1856    /// seeded as grant grains in the file itself.
1857    fn server() -> UiServer {
1858        std::env::set_var("AREEV_TEST_READER_TOK", "reader-secret");
1859        std::env::set_var("AREEV_TEST_WRITER_TOK", "writer-secret");
1860        let dir = tempfile::tempdir().unwrap();
1861        let path = dir.path().join("t.db");
1862        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
1863        store
1864            .add(&Fact::new("user:reader", REL_PERMITS, "read ON *").namespace(AUTHZ_NS).created_at(1_000))
1865            .unwrap();
1866        store
1867            .add(
1868                &Fact::new("agent:writer", REL_PERMITS, "read,write ON caller")
1869                    .namespace(AUTHZ_NS)
1870                    .created_at(2_000),
1871            )
1872            .unwrap();
1873        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller").created_at(3_000)).unwrap();
1874        std::mem::forget(dir);
1875        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
1876        let map = CredentialMap::from_json(
1877            r#"{"version":1,"tokens":[
1878                {"env":"AREEV_TEST_READER_TOK","principal":"user:reader"},
1879                {"env":"AREEV_TEST_WRITER_TOK","principal":"agent:writer"}
1880            ]}"#,
1881        )
1882        .unwrap();
1883        UiServer::new(facade, "test".into()).with_credentials(map)
1884    }
1885
1886    fn text(r: &(&str, &str, Vec<u8>)) -> String {
1887        String::from_utf8_lossy(&r.2).to_string()
1888    }
1889
1890    const READ: &[u8] = br#"{"query":"RECALL facts WHERE subject = \"acme\""}"#;
1891    const WRITE: &[u8] = br#"{"query":"ADD fact SET subject = \"x\" SET relation = \"r\" SET object = \"o\" SET namespace = \"caller\" REASON \"t\""}"#;
1892
1893    #[test]
1894    fn tokens_bind_principals_and_verbs_decide() {
1895        let s = server();
1896
1897        // The reader reads…
1898        let r = s.route("POST", "/api/cal", READ, Some("reader-secret"), None);
1899        assert!(r.0.starts_with("200"), "{}", text(&r));
1900        assert!(text(&r).contains("grains"));
1901        // …and cannot write.
1902        let w = s.route("POST", "/api/cal", WRITE, Some("reader-secret"), None);
1903        assert!(text(&w).contains("AUT-E001"), "reader write must be refused: {}", text(&w));
1904
1905        // The writer writes.
1906        let w = s.route("POST", "/api/cal", WRITE, Some("writer-secret"), None);
1907        assert!(text(&w).contains("added"), "writer write must land: {}", text(&w));
1908
1909        // Sequential isolation: the very next reader request is still
1910        // read-only — nothing leaked from the writer's binding.
1911        let w = s.route("POST", "/api/cal", WRITE, Some("reader-secret"), None);
1912        assert!(text(&w).contains("AUT-E001"), "{}", text(&w));
1913    }
1914
1915    #[test]
1916    fn anonymous_is_read_only_and_loop_run_is_verb_gated() {
1917        let s = server();
1918        // No token → anonymous: reads work…
1919        let r = s.route("POST", "/api/cal", READ, None, None);
1920        assert!(r.0.starts_with("200"), "{}", text(&r));
1921        assert!(text(&r).contains("grains"));
1922        // …writes are verb-refused…
1923        let w = s.route("POST", "/api/cal", WRITE, None, None);
1924        assert!(text(&w).contains("AUT-E001"), "{}", text(&w));
1925        // …and an unknown token is anonymous too, never an escalation.
1926        let w = s.route("POST", "/api/cal", WRITE, Some("stolen-guess"), None);
1927        assert!(text(&w).contains("AUT-E001"), "{}", text(&w));
1928
1929        // The analysis trigger needs loop.run — anonymous gets 403.
1930        let run = s.route("POST", "/api/loop/run", b"{}", None, None);
1931        assert!(run.0.starts_with("403"), "{}", text(&run));
1932    }
1933
1934    #[test]
1935    fn whoami_reports_the_bound_principal() {
1936        let s = server();
1937        let r = s.route("GET", "/api/whoami", b"", Some("reader-secret"), None);
1938        assert!(text(&r).contains("user:reader"), "{}", text(&r));
1939        assert!(text(&r).contains("credentials"));
1940        let r = s.route("GET", "/api/whoami", b"", None, None);
1941        assert!(text(&r).contains("anonymous"), "{}", text(&r));
1942    }
1943
1944    /// A credential map must not silently disable `--token-env`. Attaching
1945    /// one changes WHO a request is; the operator who also asked for a
1946    /// shared secret still gets authentication on every request.
1947    #[test]
1948    fn a_credential_map_does_not_disable_the_shared_secret() {
1949        let s = server().with_auth("console-token".into());
1950
1951        // No credential at all: refused, not dropped to `anonymous`.
1952        let r = s.route("POST", "/api/cal", READ, None, None);
1953        assert!(r.0.starts_with("401"), "{}", text(&r));
1954        let page = s.route("GET", "/", b"", None, None);
1955        assert!(page.0.starts_with("401"), "the console page is guarded too");
1956        // An unrecognized token is refused rather than downgraded.
1957        let r = s.route("POST", "/api/cal", READ, Some("stolen-guess"), None);
1958        assert!(r.0.starts_with("401"), "{}", text(&r));
1959
1960        // A map token still binds its principal and its verbs still decide.
1961        let r = s.route("POST", "/api/cal", READ, Some("reader-secret"), None);
1962        assert!(r.0.starts_with("200"), "{}", text(&r));
1963        let w = s.route("POST", "/api/cal", WRITE, Some("reader-secret"), None);
1964        assert!(text(&w).contains("AUT-E001"), "{}", text(&w));
1965
1966        // The shared secret is the implied admin (docs/cal-all-you-need
1967        // -proposal.md §5.2b) — it keeps the owner session it always had.
1968        let w = s.route("POST", "/api/cal", WRITE, Some("console-token"), None);
1969        assert!(text(&w).contains("added"), "{}", text(&w));
1970    }
1971
1972    /// `RUN LOOP` through CAL is gated exactly as `POST /api/loop/run` is —
1973    /// otherwise the CAL surface is a way around the HTTP gate.
1974    #[test]
1975    fn run_loop_via_cal_is_verb_gated_like_the_route() {
1976        let s = server();
1977        let run = s.route("POST", "/api/loop/run", b"{}", None, None);
1978        assert!(run.0.starts_with("403"), "{}", text(&run));
1979        let cal = s.route("POST", "/api/cal", br#"{"query":"RUN LOOP"}"#, None, None);
1980        assert!(
1981            text(&cal).contains("AUT-E001"),
1982            "anonymous must not run the loop through CAL either: {}",
1983            text(&cal)
1984        );
1985    }
1986}
1987
1988#[cfg(test)]
1989mod namespace_route_tests {
1990    use super::UiServer;
1991    use areev_cal::AreevFacade;
1992    use areev_core::authz::{CredentialMap, AUTHZ_NS, REL_PERMITS};
1993    use areev_core::types::{Fact, Grain};
1994    use areev_store::Areev;
1995
1996    fn text(r: &(&str, &str, Vec<u8>)) -> String {
1997        String::from_utf8_lossy(&r.2).to_string()
1998    }
1999
2000    /// `agent:writer` is granted read+write on `caller` only — nothing on
2001    /// `other`, nothing on the audit namespace.
2002    fn restricted_server() -> UiServer {
2003        std::env::set_var("AREEV_TEST_NS_WRITER_TOK", "writer-secret");
2004        let dir = tempfile::tempdir().unwrap();
2005        let path = dir.path().join("t.db");
2006        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
2007        store
2008            .add(&Fact::new("agent:writer", REL_PERMITS, "read,write ON caller").namespace(AUTHZ_NS).created_at(1_000))
2009            .unwrap();
2010        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller").created_at(2_000)).unwrap();
2011        store.add(&Fact::new("bob", "role", "admin").namespace("other").created_at(3_000)).unwrap();
2012        std::mem::forget(dir);
2013        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
2014        let map = CredentialMap::from_json(
2015            r#"{"version":1,"tokens":[{"env":"AREEV_TEST_NS_WRITER_TOK","principal":"agent:writer"}]}"#,
2016        )
2017        .unwrap();
2018        UiServer::new(facade, "test".into()).with_credentials(map)
2019    }
2020
2021    /// Unbound local open — the implicit owner, unrestricted on every
2022    /// namespace, same as a plain `areev ui` with no `--auth`.
2023    fn owner_server() -> UiServer {
2024        let dir = tempfile::tempdir().unwrap();
2025        let path = dir.path().join("t.db");
2026        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
2027        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller").created_at(1_000)).unwrap();
2028        store.add(&Fact::new("bob", "role", "admin").namespace("other").created_at(2_000)).unwrap();
2029        std::mem::forget(dir);
2030        UiServer::new(AreevFacade::with_session(store, Some("caller".into()), None), "t".into())
2031    }
2032
2033    #[test]
2034    fn namespaces_route_hides_what_the_principal_cannot_read() {
2035        let s = restricted_server();
2036        let r = s.route("GET", "/api/namespaces", b"", Some("writer-secret"), None);
2037        let body = text(&r);
2038        assert!(body.contains("\"caller\""), "granted namespace must be listed: {body}");
2039        assert!(!body.contains("\"other\""), "ungranted namespace must not be listed: {body}");
2040        assert!(!body.contains(AUTHZ_NS), "the audit namespace must not leak either: {body}");
2041    }
2042
2043    #[test]
2044    fn namespaces_route_lists_everything_for_the_owner() {
2045        let s = owner_server();
2046        let r = s.route("GET", "/api/namespaces", b"", None, None);
2047        let body = text(&r);
2048        assert!(body.contains("\"caller\"") && body.contains("\"other\""), "{body}");
2049    }
2050
2051    #[test]
2052    fn browse_ns_param_is_read_gated_for_a_restricted_principal() {
2053        let s = restricted_server();
2054        let ok = s.route("GET", "/api/browse?ns=caller", b"", Some("writer-secret"), None);
2055        assert!(text(&ok).contains("\"ok\":true"), "their own granted namespace must work: {}", text(&ok));
2056
2057        let denied = s.route("GET", "/api/browse?ns=other", b"", Some("writer-secret"), None);
2058        let body = text(&denied);
2059        assert!(body.contains("\"ok\":false"), "an ungranted namespace must be refused, not silently emptied: {body}");
2060    }
2061
2062    #[test]
2063    fn browse_ns_star_requires_owner() {
2064        let s = restricted_server();
2065        let r = s.route("GET", "/api/browse?ns=*", b"", Some("writer-secret"), None);
2066        assert!(text(&r).contains("\"ok\":false"), "a restricted principal must not get the unfiltered view: {}", text(&r));
2067
2068        let owner = owner_server();
2069        let r2 = owner.route("GET", "/api/browse?ns=*", b"", None, None);
2070        let body2 = text(&r2);
2071        assert!(body2.contains("\"ok\":true"), "the owner session may lift the filter: {body2}");
2072        assert!(body2.contains("\"caller\"") && body2.contains("\"other\""), "{body2}");
2073    }
2074
2075    #[test]
2076    fn browse_without_ns_param_keeps_original_session_default_behavior() {
2077        let s = owner_server();
2078        let r = s.route("GET", "/api/browse", b"", None, None);
2079        let body = text(&r);
2080        assert!(body.contains("\"caller\""), "{body}");
2081        assert!(!body.contains("\"other\""), "omitting ?ns must behave exactly as before this feature existed: {body}");
2082    }
2083}
2084
2085#[cfg(test)]
2086mod builder_tests {
2087    use super::UiServer;
2088    use areev_cal::AreevFacade;
2089    use areev_store::Areev;
2090
2091    fn server() -> UiServer {
2092        let dir = tempfile::tempdir().unwrap();
2093        let path = dir.path().join("t.db");
2094        let store = Areev::open(path.to_str().unwrap()).unwrap();
2095        std::mem::forget(dir);
2096        UiServer::new(AreevFacade::with_session(store, Some("caller".into()), None), "t".into())
2097    }
2098
2099    /// `areev ui --no-destructive-ops --policy FILE` calls these in this
2100    /// order. A later builder must not re-enable destruction the operator
2101    /// turned off — the console was announced as read-only.
2102    #[test]
2103    fn attaching_a_loop_policy_preserves_the_destructive_cap() {
2104        let s = server()
2105            .allow_destructive_ops(false)
2106            .with_loop_policy(areev_loop::Policy::default());
2107        let r = s.route(
2108            "POST",
2109            "/api/cal",
2110            br#"{"query":"FORGET sha256:684c6c9bda818630a870119d0726e4d242ed537af061658ef6f3acb158a2c67d"}"#,
2111            None,
2112            None,
2113        );
2114        let body = String::from_utf8_lossy(&r.2).to_string();
2115        assert!(
2116            body.contains("--no-destructive-ops") || r.0.starts_with("401"),
2117            "destruction must stay disabled: {body}"
2118        );
2119    }
2120}
2121
2122#[cfg(test)]
2123mod run_api_tests {
2124    use super::UiServer;
2125    use areev_cal::AreevFacade;
2126    use areev_core::authz::{CredentialMap, AUTHZ_NS, REL_PERMITS};
2127    use areev_core::types::{Fact, Grain, Tool, ToolKind, Workflow};
2128    use areev_store::Areev;
2129    use std::sync::Arc;
2130
2131    /// A server whose file holds a PARKED run (Client-gated approve node) —
2132    /// the HITL queue's fixture. Returns (server, ask id).
2133    fn parked_server() -> (UiServer, String) {
2134        std::env::set_var("AREEV_RUN_OFFICER_TOK", "officer-secret");
2135        std::env::set_var("AREEV_RUN_STARTER_TOK", "starter-secret");
2136        let dir = tempfile::tempdir().unwrap();
2137        let path = dir.path().join("t.db");
2138        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
2139        for (who, what) in [
2140            ("user:officer", "read,run.respond ON ops"),
2141            ("user:starter", "read ON ops"),
2142        ] {
2143            store
2144                .add(&Fact::new(who, REL_PERMITS, what).namespace(AUTHZ_NS).created_at(900))
2145                .unwrap();
2146        }
2147        std::mem::forget(dir);
2148        let facade = Arc::new(AreevFacade::with_session(store, Some("ops".into()), None));
2149
2150        // Seed + run the plan to the park with a scripted runner.
2151        let (wf, _g, _a) = facade
2152            .with_store(|m| {
2153                let greet = Tool::new("greet")
2154                    .kind(ToolKind::Definition)
2155                    .tool_description("t")
2156                    .created_at(500)
2157                    .namespace("ops");
2158                let gh = m.add(&greet)?;
2159                let approve = Tool::new("approve")
2160                    .kind(ToolKind::Definition)
2161                    .tool_description("human approves")
2162                    .executor_kind(areev_core::types::ExecutorKind::Client)
2163                    .created_at(501)
2164                    .namespace("ops");
2165                let ah = m.add(&approve)?;
2166                let wf = Workflow::new(vec!["greet".into(), "approve".into()])
2167                    .edge("greet", "approve")
2168                    .bind("greet", &gh.to_hex())
2169                    .bind("approve", &ah.to_hex())
2170                    .created_at(502)
2171                    .namespace("ops");
2172                let wh = m.add(&wf)?;
2173                Ok::<_, areev_core::error::AreevError>((wh, gh, ah))
2174            })
2175            .unwrap();
2176        struct Ok1;
2177        impl areev_run::HostToolExecutor for Ok1 {
2178            fn execute(
2179                &self,
2180                t: &str,
2181                _h: &str,
2182                _i: &serde_json::Value,
2183                _k: &str,
2184            ) -> areev_run::ExecResult {
2185                areev_run::ExecResult::Ok(serde_json::json!({t.to_string(): true}))
2186            }
2187        }
2188        let runner = areev_run::Runner {
2189            facade: Arc::clone(&facade),
2190            clock: Arc::new(areev_run::ScriptedClock::new(
2191                (0..100).map(|i| 1_758_000_000_000 + i * 10).collect(),
2192            )),
2193            executor: Arc::new(Ok1),
2194            llm: None,
2195            observer: None,
2196            ns: "ops".into(),
2197            principal: "user:starter".into(),
2198        };
2199        let session = runner
2200            .start(&wf, "hitl-1", serde_json::json!({}), &areev_run::RunOptions {
2201                workers: 1,
2202                ..Default::default()
2203            })
2204            .unwrap();
2205        let areev_run::RunSession::Parked { envelope, .. } = session else {
2206            panic!("expected park")
2207        };
2208        let ask = envelope["asks"][0]["tool_call_id"].as_str().unwrap().to_string();
2209
2210        // Rebuild a facade-owning server over the SAME file is impossible
2211        // (single writer) — hand the server the same Arc'd facade via the
2212        // Arc-aware constructor path: UiServer::new takes AreevFacade, so
2213        // we build the server FIRST in real deployments; here we reuse the
2214        // internal Arc through a second UiServer field write. Instead:
2215        // drop the runner (facade Arc count drops) and unwrap.
2216        drop(runner);
2217        let facade = Arc::try_unwrap(facade).unwrap_or_else(|_| panic!("facade still shared"));
2218        let map = CredentialMap::from_json(
2219            r#"{"version":1,"tokens":[
2220                {"env":"AREEV_RUN_OFFICER_TOK","principal":"user:officer"},
2221                {"env":"AREEV_RUN_STARTER_TOK","principal":"user:starter"}
2222            ]}"#,
2223        )
2224        .unwrap();
2225        let server = UiServer::new(facade, "test".into())
2226            .with_auth("shared-console-secret".into())
2227            .with_credentials(map);
2228        (server, ask)
2229    }
2230
2231    fn body(run_id: &str, ask: &str) -> Vec<u8> {
2232        serde_json::json!({"run_id": run_id, "tool_call_id": ask, "result": {"approved": true}})
2233            .to_string()
2234            .into_bytes()
2235    }
2236
2237    #[test]
2238    fn shared_token_approvals_are_refused_and_principals_approve() {
2239        let (s, ask) = parked_server();
2240
2241        // The HITL queue is visible.
2242        let r = s.route("GET", "/api/run/list", b"", Some("shared-console-secret"), None);
2243        assert!(r.0.starts_with("200"), "{}", String::from_utf8_lossy(&r.2));
2244        let r = s.route(
2245            "GET",
2246            "/api/run/inspect?run_id=hitl-1",
2247            b"",
2248            Some("shared-console-secret"),
2249            None,
2250        );
2251        assert!(String::from_utf8_lossy(&r.2).contains(&ask), "ask visible in inspect");
2252
2253        // 1. The SHARED console secret cannot approve — identity is the point.
2254        let r = s.route("POST", "/api/run/respond", &body("hitl-1", &ask), Some("shared-console-secret"), None);
2255        assert!(r.0.starts_with("403"), "shared-token approval must refuse: {}", r.0);
2256
2257        // 2. The STARTER's own credential cannot approve their own run
2258        //    (separation of duties, enforced by the runtime).
2259        let r = s.route("POST", "/api/run/respond", &body("hitl-1", &ask), Some("starter-secret"), None);
2260        let text = String::from_utf8_lossy(&r.2).to_string();
2261        assert!(text.contains("responder") || text.contains("RUN-E012"), "{text}");
2262
2263        // 3. The officer's principal-bound credential approves.
2264        let r = s.route("POST", "/api/run/respond", &body("hitl-1", &ask), Some("officer-secret"), None);
2265        assert!(r.0.starts_with("200"), "{}", String::from_utf8_lossy(&r.2));
2266        assert!(String::from_utf8_lossy(&r.2).contains("user:officer"));
2267
2268        // 4. The kill switch keeps its low bar: shared token may cancel.
2269        let r = s.route(
2270            "POST",
2271            "/api/run/cancel",
2272            br#"{"run_id":"hitl-1","because":"test brake"}"#,
2273            Some("shared-console-secret"),
2274            None,
2275        );
2276        assert!(r.0.starts_with("200"), "{}", String::from_utf8_lossy(&r.2));
2277    }
2278}
2279
2280#[cfg(test)]
2281mod memory_scoped_token_tests {
2282    use super::UiServer;
2283    use areev_cal::AreevFacade;
2284    use areev_core::authz::{CredentialMap, AUTHZ_NS, REL_PERMITS};
2285    use areev_core::types::{Fact, Grain};
2286    use areev_store::Areev;
2287
2288    /// The enterprise-plane hub rule: one auth file, per-memory token
2289    /// scopes — a token listed for memory A is an UNKNOWN token on
2290    /// memory B's server.
2291    #[test]
2292    fn memory_scoped_tokens_do_not_cross_memories() {
2293        std::env::set_var("AREEV_SCOPED_TOK", "scoped-secret");
2294        let map_json = r#"{"version":1,"tokens":[
2295            {"env":"AREEV_SCOPED_TOK","principal":"agent:sync","memories":["memory-a"]}
2296        ]}"#;
2297        let server_for = |label: &str| {
2298            let dir = tempfile::tempdir().unwrap();
2299            let mut store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
2300            store
2301                .add(
2302                    &Fact::new("agent:sync", REL_PERMITS, "read,write ON caller")
2303                        .namespace(AUTHZ_NS)
2304                        .created_at(1_000),
2305                )
2306                .unwrap();
2307            store
2308                .add(&Fact::new("acme", "tier", "Enterprise").namespace("caller").created_at(2_000))
2309                .unwrap();
2310            std::mem::forget(dir);
2311            let facade = AreevFacade::with_session(store, Some("caller".into()), None);
2312            UiServer::new(facade, label.into())
2313                .with_credentials(CredentialMap::from_json(map_json).unwrap())
2314        };
2315        const WRITE: &[u8] = br#"{"query":"ADD fact SET subject = \"x\" SET relation = \"r\" SET object = \"o\" SET namespace = \"caller\" REASON \"t\""}"#;
2316
2317        // On memory-a the token authenticates and its grants let the write
2318        // through (no authorization refusal in the CAL payload).
2319        let a = server_for("memory-a");
2320        let r = a.route("POST", "/api/cal", WRITE, Some("scoped-secret"), None);
2321        let text = String::from_utf8_lossy(&r.2).to_string();
2322        assert!(r.0.starts_with("200") && !text.contains("AUT-E001"), "{text}");
2323
2324        // On memory-b the SAME token is an unknown credential: the request
2325        // runs as anonymous and the write is refused (AUT-E001 rides the
2326        // CAL payload — the console surfaces statement results in-band).
2327        let b = server_for("memory-b");
2328        let r = b.route("POST", "/api/cal", WRITE, Some("scoped-secret"), None);
2329        let text = String::from_utf8_lossy(&r.2).to_string();
2330        assert!(
2331            text.contains("AUT-E001") && text.contains("anonymous"),
2332            "scoped token must not cross memories: {text}"
2333        );
2334
2335        // An auth file with an EMPTY memories scope refuses to load.
2336        assert!(CredentialMap::from_json(
2337            r#"{"version":1,"tokens":[{"env":"AREEV_SCOPED_TOK","principal":"x","memories":[]}]}"#
2338        )
2339        .is_err());
2340    }
2341}
2342
2343#[cfg(all(test, feature = "tls"))]
2344mod tls_tests {
2345    use super::UiServer;
2346    use areev_cal::AreevFacade;
2347    use areev_store::Areev;
2348    use std::io::{Read, Write};
2349
2350    fn self_signed(dir: &std::path::Path) -> Option<(String, String)> {
2351        let cert = dir.join("cert.pem");
2352        let key = dir.join("key.pem");
2353        let status = std::process::Command::new("openssl")
2354            .args([
2355                "req", "-x509", "-newkey", "rsa:2048", "-nodes",
2356                "-keyout", key.to_str().unwrap(), "-out", cert.to_str().unwrap(),
2357                "-days", "1", "-subj", "/CN=localhost",
2358                "-addext", "subjectAltName=DNS:localhost,IP:127.0.0.1",
2359                "-addext", "basicConstraints=critical,CA:FALSE",
2360            ])
2361            .stdout(std::process::Stdio::null())
2362            .stderr(std::process::Stdio::null())
2363            .status();
2364        match status {
2365            Ok(s) if s.success() => Some((
2366                cert.to_str().unwrap().to_string(),
2367                key.to_str().unwrap().to_string(),
2368            )),
2369            _ => None, // no openssl on this host: the CI leg covers it
2370        }
2371    }
2372
2373    /// The §3.2 gate: a TLS round-trip succeeds, and a PLAINTEXT client on
2374    /// the same listener is refused (no silent downgrade).
2375    #[test]
2376    fn tls_round_trip_and_no_plaintext_downgrade() {
2377        let dir = tempfile::tempdir().unwrap();
2378        let Some((cert, key)) = self_signed(dir.path()) else {
2379            eprintln!("skipping: no openssl binary");
2380            return;
2381        };
2382        let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
2383        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
2384        let server = UiServer::new(facade, "tls-test".into())
2385            .with_tls(&cert, &key)
2386            .unwrap();
2387        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
2388        let addr = listener.local_addr().unwrap();
2389        let handle = std::thread::spawn(move || {
2390            // Two connections: the TLS client, then the plaintext prober.
2391            for _ in 0..2 {
2392                if let Ok((s, _)) = listener.accept() {
2393                    let _ = server.handle(s);
2394                }
2395            }
2396        });
2397
2398        // 1. TLS round-trip: trust exactly our self-signed cert.
2399        let mut roots = rustls::RootCertStore::empty();
2400        {
2401            use rustls::pki_types::pem::PemObject;
2402            for c in rustls::pki_types::CertificateDer::pem_file_iter(&cert).unwrap() {
2403                roots.add(c.unwrap()).unwrap();
2404            }
2405        }
2406        let config = std::sync::Arc::new(
2407            rustls::ClientConfig::builder()
2408                .with_root_certificates(roots)
2409                .with_no_client_auth(),
2410        );
2411        let name = rustls::pki_types::ServerName::try_from("localhost").unwrap();
2412        let session = rustls::ClientConnection::new(config, name).unwrap();
2413        let tcp = std::net::TcpStream::connect(addr).unwrap();
2414        let mut tls = rustls::StreamOwned::new(session, tcp);
2415        tls.write_all(b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n")
2416            .unwrap();
2417        let mut response = String::new();
2418        let _ = tls.read_to_string(&mut response);
2419        assert!(response.starts_with("HTTP/1.1 200"), "{}", &response[..response.len().min(120)]);
2420        assert!(response.contains("Areev"), "console served over TLS");
2421
2422        // 2. No downgrade: plaintext HTTP on the TLS listener gets a
2423        // handshake failure, never a page.
2424        let mut plain = std::net::TcpStream::connect(addr).unwrap();
2425        plain
2426            .write_all(b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n")
2427            .unwrap();
2428        let mut buf = Vec::new();
2429        let _ = plain.read_to_end(&mut buf);
2430        let text = String::from_utf8_lossy(&buf);
2431        assert!(
2432            !text.contains("HTTP/1.1 200"),
2433            "plaintext must not be served on a TLS listener: {text}"
2434        );
2435        handle.join().unwrap();
2436    }
2437
2438    /// `with_tls` composes with `into_hub` the same way it does with plain
2439    /// `ui` mode — `into_hub` only sets the token/segment-dir/allow-remote
2440    /// fields, not the connection layer, so a hub server terminates TLS
2441    /// identically. Pins the CLI wiring that lets `areev hub --tls-cert
2442    /// ... --tls-key ...` work, not just `areev ui`.
2443    #[test]
2444    fn hub_mode_terminates_tls_the_same_way_as_ui_mode() {
2445        let dir = tempfile::tempdir().unwrap();
2446        let Some((cert, key)) = self_signed(dir.path()) else {
2447            eprintln!("skipping: no openssl binary");
2448            return;
2449        };
2450        let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
2451        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
2452        let segments = dir.path().join("segments");
2453        let server = UiServer::new(facade, "hub-tls-test".into())
2454            .into_hub(Some("hub-token".into()), segments.to_str().unwrap())
2455            .unwrap()
2456            .with_tls(&cert, &key)
2457            .unwrap();
2458        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
2459        let addr = listener.local_addr().unwrap();
2460        let handle = std::thread::spawn(move || {
2461            if let Ok((s, _)) = listener.accept() {
2462                let _ = server.handle(s);
2463            }
2464        });
2465
2466        let mut roots = rustls::RootCertStore::empty();
2467        {
2468            use rustls::pki_types::pem::PemObject;
2469            for c in rustls::pki_types::CertificateDer::pem_file_iter(&cert).unwrap() {
2470                roots.add(c.unwrap()).unwrap();
2471            }
2472        }
2473        let config = std::sync::Arc::new(
2474            rustls::ClientConfig::builder()
2475                .with_root_certificates(roots)
2476                .with_no_client_auth(),
2477        );
2478        let name = rustls::pki_types::ServerName::try_from("localhost").unwrap();
2479        let session = rustls::ClientConnection::new(config, name).unwrap();
2480        let tcp = std::net::TcpStream::connect(addr).unwrap();
2481        let mut tls = rustls::StreamOwned::new(session, tcp);
2482        tls.write_all(b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n")
2483            .unwrap();
2484        let mut response = String::new();
2485        let _ = tls.read_to_string(&mut response);
2486        assert!(
2487            response.starts_with("HTTP/1.1 200"),
2488            "hub should terminate TLS the same way ui does: {}",
2489            &response[..response.len().min(120)]
2490        );
2491        handle.join().unwrap();
2492    }
2493}
2494
2495#[cfg(test)]
2496mod sso_tests {
2497    use super::UiServer;
2498    use areev_cal::AreevFacade;
2499    use areev_core::authz::{AUTHZ_NS, REL_PERMITS};
2500    use areev_core::types::{Fact, Grain};
2501    use areev_store::Areev;
2502    use std::io::{Read, Write};
2503
2504    const WRITE: &[u8] = br#"{"query":"ADD fact SET subject = \"x\" SET relation = \"r\" SET object = \"o\" SET namespace = \"caller\" REASON \"t\""}"#;
2505
2506    fn sso_server() -> UiServer {
2507        let dir = tempfile::tempdir().unwrap();
2508        let mut store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
2509        store
2510            .add(
2511                &Fact::new("user:pat@example.com", REL_PERMITS, "read,write ON caller")
2512                    .namespace(AUTHZ_NS)
2513                    .created_at(1_000),
2514            )
2515            .unwrap();
2516        std::mem::forget(dir);
2517        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
2518        UiServer::new(facade, "sso-test".into()).with_sso("x-forwarded-user", "proxy-secret")
2519    }
2520
2521    fn post(server: &UiServer, extra_headers: &str) -> String {
2522        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
2523        let addr = listener.local_addr().unwrap();
2524        std::thread::scope(|scope| {
2525            scope.spawn(|| {
2526                if let Ok((s, _)) = listener.accept() {
2527                    let _ = server.handle(s);
2528                }
2529            });
2530            let mut c = std::net::TcpStream::connect(addr).unwrap();
2531            let req = format!(
2532                "POST /api/cal HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Type: application/json\r\n{extra_headers}Content-Length: {}\r\nConnection: close\r\n\r\n",
2533                WRITE.len()
2534            );
2535            c.write_all(req.as_bytes()).unwrap();
2536            c.write_all(WRITE).unwrap();
2537            let mut out = String::new();
2538            let _ = c.read_to_string(&mut out);
2539            out
2540        })
2541    }
2542
2543    /// The §3.3 gate: a proxy-proven identity lands as a named actor whose
2544    /// FILE grants decide; a forged identity header without the proxy
2545    /// secret is ignored (the write is refused as anonymous).
2546    #[test]
2547    fn proxied_identity_works_and_forged_headers_are_ignored() {
2548        let server = sso_server();
2549
2550        // 1. Proxy-proven: identity + secret → the granted principal writes.
2551        let ok = post(
2552            &server,
2553            "X-Forwarded-User: user:pat@example.com\r\nX-Areev-Proxy-Secret: proxy-secret\r\n",
2554        );
2555        assert!(ok.contains("HTTP/1.1 200"), "{}", &ok[..ok.len().min(200)]);
2556        assert!(!ok.contains("AUT-E001"), "granted principal must write: {ok}");
2557
2558        // 2. Forged: the identity header WITHOUT the secret is ignored — the
2559        // request is anonymous and the write refuses.
2560        let forged = post(&server, "X-Forwarded-User: user:pat@example.com\r\n");
2561        assert!(
2562            forged.contains("anonymous") || forged.contains("401"),
2563            "forged identity must not authenticate: {forged}"
2564        );
2565
2566        // 3. Wrong secret: same refusal (constant-time compare, same path).
2567        let wrong = post(
2568            &server,
2569            "X-Forwarded-User: user:pat@example.com\r\nX-Areev-Proxy-Secret: guess\r\n",
2570        );
2571        assert!(
2572            wrong.contains("anonymous") || wrong.contains("401"),
2573            "wrong proxy secret must not authenticate: {wrong}"
2574        );
2575    }
2576
2577    use crate::CONSOLE_HTML;
2578    use std::collections::BTreeSet;
2579
2580    /// Every `<section class="page" id="page-X">` must be named in the one
2581    /// array in `render()` that clears `hidden`, and every sidebar
2582    /// `data-page="X"` must have such a section.
2583    ///
2584    /// The console's page sections ship `hidden` in the markup, so a page is
2585    /// visible only if `render()` un-hides it. The Triggers tab shipped
2586    /// missing from that array: `renderTriggers()` filled `#trgBody` on every
2587    /// render, the nav item highlighted, the hash routed — and the section
2588    /// stayed `hidden` while every other page was hidden too, so the tab
2589    /// showed a blank pane. Nothing else in the file could catch it: the
2590    /// omission is a *missing* string, not a wrong one.
2591    #[test]
2592    fn every_console_page_is_in_the_visibility_list() {
2593        fn all(hay: &str, open: &str) -> BTreeSet<String> {
2594            let mut out = BTreeSet::new();
2595            let mut rest = hay;
2596            while let Some(i) = rest.find(open) {
2597                rest = &rest[i + open.len()..];
2598                let end = rest.find('"').expect("unterminated attribute");
2599                out.insert(rest[..end].to_string());
2600                rest = &rest[end..];
2601            }
2602            out
2603        }
2604
2605        let sections = all(CONSOLE_HTML, "id=\"page-");
2606        let nav = all(CONSOLE_HTML, "data-page=\"");
2607        for want in ["workflows", "runs", "tools"] {
2608            assert!(sections.contains(want), "the {want} section itself went missing");
2609        }
2610        // Triggers deliberately have no page: a trigger points AT a plan and is
2611        // only legible next to it, so they render in the workflow canvas's own
2612        // lane. The old deep link must still land somewhere real.
2613        assert!(!sections.contains("triggers"), "triggers belong on the canvas, not on a page");
2614        assert!(
2615            CONSOLE_HTML.contains("if (head === 'triggers')"),
2616            "#triggers must still route somewhere — a removed page is not a dead bookmark"
2617        );
2618
2619        // The single array in render() that drives `.hidden`.
2620        let head = "for (const p of [";
2621        let i = CONSOLE_HTML.find(head).expect("render()'s page-visibility loop");
2622        let tail = &CONSOLE_HTML[i + head.len()..];
2623        let j = tail.find(']').expect("unterminated page array");
2624        let listed: BTreeSet<String> = tail[..j]
2625            .split(',')
2626            .map(|t| t.trim().trim_matches('\'').to_string())
2627            .filter(|t| !t.is_empty())
2628            .collect();
2629
2630        assert_eq!(
2631            sections, listed,
2632            "page sections and render()'s visibility list disagree; a section missing \
2633             from the list renders into a pane that never un-hides"
2634        );
2635        let orphan: Vec<_> = nav.difference(&sections).collect();
2636        assert!(orphan.is_empty(), "nav items with no page section: {orphan:?}");
2637    }
2638}