nescio 0.6.0

nescioDB (Aporia): a database whose primary object is ignorance — evidence with decay, regions with entropy, and queries that plan their own data procurement.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! `nescio serve`: the database as an HTTP/JSON service.
//!
//! One process owns the database directory. Queries (including the
//! read-only POST /resolve) run concurrently under a shared read lock;
//! mutations take the write lock exclusively — many readers, one writer.
//!
//! Routes (all responses JSON):
//!
//! ```text
//! GET  /health
//! GET  /status
//! GET  /bound?entity=&slot=[&at=][&credible=]
//! GET  /sample?entity=[&seed=][&at=]
//! GET  /certainly?entity=&slot=&op=gt|lt|between|is|is_not[&value=][&lo=&hi=][&at=]
//! GET  /find?slot=&lo=&hi=[&mode=possible|certain][&at=]
//! POST /join           {predicate: {op, left, right, tol?}, options?, at?}
//! POST /ingest         {entity, claim, source, at?}
//! POST /ingest-batch   [{entity, claim, source, at?}, ...]  (group commit)
//! POST /resolve        {entity, slot, target_bits, actions, max_steps?, mc?, seed?, at?}
//! POST /decide         {entity, slot, objective, target, actions, max_steps?, mc?, seed?, at?}
//! POST /sources        {name, reliability, half_life_days?, axiomatic?}
//! POST /forget-source  {source}
//! POST /recalibrate    {source, apply?, min_truth_reliability?}
//! POST /priors/register {name, slot, weights}
//! POST /priors/use      {entity, slot, name}
//! ```
//!
//! `at` accepts "YYYY-MM-DD", a date-time, or unix seconds; default now.
//! Axiom conflicts map to HTTP 409 — contradiction is a real state, not a
//! server failure.

use std::collections::BTreeMap;

use serde::Deserialize;
use serde_json::json;

use crate::engine::{
    FindMode, JoinOptions, JoinPredicate, Objective, Predicate, ProcurementAction, Query,
};
use crate::error::{Error, Result};
use crate::model::evidence::{Claim, EvidenceRecord, Source};
use crate::store::Db;
use crate::time::{now_unix, parse_when};

const MAX_BODY_BYTES: usize = 8 * 1024 * 1024;

pub struct NescioServer {
    inner: tiny_http::Server,
}

impl NescioServer {
    pub fn bind(addr: &str) -> Result<Self> {
        let inner = tiny_http::Server::http(addr)
            .map_err(|e| Error::Invalid(format!("cannot bind {addr}: {e}")))?;
        Ok(NescioServer { inner })
    }

    pub fn port(&self) -> u16 {
        self.inner
            .server_addr()
            .to_ip()
            .map(|a| a.port())
            .unwrap_or(0)
    }

    /// Serve forever on a small thread pool. Queries (including the
    /// read-only POST /resolve) run concurrently under a read lock;
    /// mutations take the write lock exclusively — many readers, one
    /// writer, single process owning the directory.
    pub fn run(self, db: Db) -> Result<()> {
        let server = std::sync::Arc::new(self.inner);
        let db = std::sync::Arc::new(std::sync::RwLock::new(db));
        let workers = std::thread::available_parallelism()
            .map(|n| n.get().clamp(2, 8))
            .unwrap_or(4);
        let mut handles = Vec::new();
        for _ in 0..workers {
            let server = server.clone();
            let db = db.clone();
            handles.push(std::thread::spawn(move || {
                while let Ok(mut request) = server.recv() {
                    let method = request.method().as_str().to_string();
                    let url = request.url().to_string();
                    let (path, query) = match url.split_once('?') {
                        Some((p, q)) => (p.to_string(), q.to_string()),
                        None => (url, String::new()),
                    };
                    let body = if request.body_length().unwrap_or(0) > MAX_BODY_BYTES {
                        None
                    } else {
                        let mut s = String::new();
                        match request.as_reader().read_to_string(&mut s) {
                            Ok(_) => Some(s),
                            Err(_) => Some(String::new()),
                        }
                    };
                    let (status, payload) = match body {
                        None => (413, json!({"error": "body too large"}).to_string()),
                        Some(b) => {
                            if is_write(&method, &path) {
                                let mut guard = db.write().expect("db lock poisoned");
                                handle(&mut guard, &method, &path, &query, &b)
                            } else {
                                let guard = db.read().expect("db lock poisoned");
                                let params = parse_query(&query);
                                to_response(route_read(&guard, &method, &path, &params, &b))
                            }
                        }
                    };
                    let header = tiny_http::Header::from_bytes(
                        &b"Content-Type"[..],
                        &b"application/json"[..],
                    )
                    .expect("static header");
                    let response = tiny_http::Response::from_string(payload)
                        .with_status_code(status)
                        .with_header(header);
                    let _ = request.respond(response);
                }
            }));
        }
        for h in handles {
            let _ = h.join();
        }
        Ok(())
    }
}

/// Routes that mutate the database and need the exclusive write lock.
/// POST /resolve, /decide and /join only read (they plan / match), so they
/// run under the shared read lock alongside the GET verbs.
fn is_write(method: &str, path: &str) -> bool {
    method == "POST" && !matches!(path, "/resolve" | "/decide" | "/join")
}

/// Pure request handler: (method, path, query, body) -> (status, JSON).
pub fn handle(db: &mut Db, method: &str, path: &str, query: &str, body: &str) -> (u16, String) {
    let params = parse_query(query);
    let result = if is_write(method, path) {
        route_write(db, method, path, body)
    } else {
        route_read(db, method, path, &params, body)
    };
    to_response(result)
}

fn to_response(result: Result<String>) -> (u16, String) {
    match result {
        Ok(payload) => (200, payload),
        Err(Error::AxiomConflict(m)) => (
            409,
            json!({"error": format!("axiom conflict: {m}")}).to_string(),
        ),
        Err(Error::Invalid(m)) => {
            let status = if m.starts_with("no such route") {
                404
            } else {
                400
            };
            (status, json!({"error": m}).to_string())
        }
        Err(e) => (500, json!({"error": e.to_string()}).to_string()),
    }
}

fn route_read(
    db: &Db,
    method: &str,
    path: &str,
    params: &BTreeMap<String, String>,
    body: &str,
) -> Result<String> {
    match (method, path) {
        ("GET", "/health") => {
            Ok(json!({"ok": true, "version": env!("CARGO_PKG_VERSION")}).to_string())
        }
        ("GET", "/status") => {
            let sources: Vec<&Source> = db.sources.values().collect();
            Ok(serde_json::to_string(&json!({
                "evidence": db.evidence.len(),
                "entities": db.entities().count(),
                "slots": db.schema.slots,
                "couplings": db.schema.couplings.iter().map(|c| c.label()).collect::<Vec<_>>(),
                "sources": sources,
            }))?)
        }
        ("GET", "/bound") => {
            let q = Query::new(db, at_param(params)?);
            let credible = num_param(params, "credible")?.unwrap_or(0.95);
            let b = q.bound(
                req_param(params, "entity")?,
                req_param(params, "slot")?,
                credible,
            )?;
            Ok(serde_json::to_string(&b)?)
        }
        ("GET", "/sample") => {
            let q = Query::new(db, at_param(params)?);
            let seed = num_param(params, "seed")?.unwrap_or(0.0) as u64;
            let world = q.sample(req_param(params, "entity")?, seed)?;
            Ok(serde_json::to_string(&world)?)
        }
        ("GET", "/certainly") => {
            let q = Query::new(db, at_param(params)?);
            let pred = predicate_from(params)?;
            let tri = q.certainly(
                req_param(params, "entity")?,
                req_param(params, "slot")?,
                &pred,
            )?;
            Ok(serde_json::to_string(&json!({"result": tri}))?)
        }
        ("GET", "/find") => {
            let q = Query::new(db, at_param(params)?);
            let lo = num_param(params, "lo")?
                .ok_or_else(|| Error::Invalid("missing param lo".into()))?;
            let hi = num_param(params, "hi")?
                .ok_or_else(|| Error::Invalid("missing param hi".into()))?;
            let mode: FindMode = params
                .get("mode")
                .map_or("possible", |s| s.as_str())
                .parse()?;
            let found = q.find(req_param(params, "slot")?, lo, hi, mode)?;
            Ok(serde_json::to_string(&found)?)
        }
        ("POST", "/resolve") => {
            #[derive(Deserialize)]
            struct Body {
                entity: String,
                slot: String,
                target_bits: f64,
                actions: Vec<ProcurementAction>,
                max_steps: Option<usize>,
                mc: Option<usize>,
                seed: Option<u64>,
                at: Option<serde_json::Value>,
            }
            let b: Body = from_body(body)?;
            let q = Query::new(db, at_value(b.at)?);
            let plan = q.resolve(
                &b.entity,
                &b.slot,
                b.target_bits,
                &b.actions,
                b.max_steps.unwrap_or(10),
                b.mc.unwrap_or(12),
                b.seed.unwrap_or(0),
            )?;
            Ok(serde_json::to_string(&plan)?)
        }
        ("POST", "/decide") => {
            #[derive(Deserialize)]
            struct Body {
                entity: String,
                slot: String,
                objective: Objective,
                target: f64,
                actions: Vec<ProcurementAction>,
                max_steps: Option<usize>,
                mc: Option<usize>,
                seed: Option<u64>,
                at: Option<serde_json::Value>,
            }
            let b: Body = from_body(body)?;
            let q = Query::new(db, at_value(b.at)?);
            let plan = q.resolve_decision(
                &b.entity,
                &b.slot,
                &b.objective,
                b.target,
                &b.actions,
                b.max_steps.unwrap_or(10),
                b.mc.unwrap_or(12),
                b.seed.unwrap_or(0),
            )?;
            Ok(serde_json::to_string(&plan)?)
        }
        ("POST", "/join") => {
            #[derive(Deserialize)]
            struct Body {
                predicate: JoinPredicate,
                #[serde(default)]
                options: JoinOptions,
                #[serde(default)]
                at: Option<serde_json::Value>,
            }
            let b: Body = from_body(body)?;
            let q = Query::new(db, at_value(b.at)?);
            let res = q.join(&b.predicate, &b.options)?;
            Ok(serde_json::to_string(&res)?)
        }
        _ => Err(Error::Invalid(format!("no such route: {method} {path}"))),
    }
}

#[derive(Deserialize)]
struct IngestBody {
    entity: String,
    claim: Claim,
    source: String,
    at: Option<serde_json::Value>,
}

impl IngestBody {
    fn into_record(self) -> Result<EvidenceRecord> {
        Ok(EvidenceRecord {
            entity: self.entity,
            claim: self.claim,
            source: self.source,
            observed_at: at_value(self.at)?,
        })
    }
}

fn route_write(db: &mut Db, method: &str, path: &str, body: &str) -> Result<String> {
    match (method, path) {
        ("POST", "/ingest") => {
            let b: IngestBody = from_body(body)?;
            let rec = b.into_record()?;
            let observed_at = rec.observed_at;
            db.ingest(rec)?;
            Ok(json!({"ok": true, "observed_at": observed_at}).to_string())
        }
        ("POST", "/ingest-batch") => {
            let bodies: Vec<IngestBody> = from_body(body)?;
            let recs = bodies
                .into_iter()
                .map(IngestBody::into_record)
                .collect::<Result<Vec<_>>>()?;
            let n = db.ingest_batch(recs)?;
            Ok(json!({"ok": true, "ingested": n}).to_string())
        }
        ("POST", "/sources") => {
            let source: Source = from_body(body)?;
            let n = db.put_source(source)?;
            Ok(json!({"ok": true, "reinterpreted": n}).to_string())
        }
        ("POST", "/forget-source") => {
            #[derive(Deserialize)]
            struct Body {
                source: String,
            }
            let b: Body = from_body(body)?;
            let removed = db.forget_source(&b.source)?;
            Ok(json!({"ok": true, "erased": removed}).to_string())
        }
        ("POST", "/recalibrate") => {
            #[derive(Deserialize)]
            struct Body {
                source: String,
                apply: Option<bool>,
                min_truth_reliability: Option<f64>,
            }
            let b: Body = from_body(body)?;
            let fit = db.recalibrate_source(&b.source, b.min_truth_reliability.unwrap_or(0.99))?;
            let mut applied = 0;
            if b.apply.unwrap_or(false) {
                let axiomatic = db.sources.get(&b.source).is_some_and(|s| s.axiomatic);
                applied = db.put_source(Source {
                    name: b.source.clone(),
                    reliability: fit.r0,
                    half_life_days: fit.half_life_days,
                    axiomatic,
                })?;
            }
            Ok(serde_json::to_string(
                &json!({"fit": fit, "applied": applied}),
            )?)
        }
        ("POST", "/priors/register") => {
            #[derive(Deserialize)]
            struct Body {
                name: String,
                slot: String,
                weights: Vec<f64>,
            }
            let b: Body = from_body(body)?;
            db.register_prior(&b.name, &b.slot, b.weights)?;
            Ok(json!({"ok": true}).to_string())
        }
        ("POST", "/priors/use") => {
            #[derive(Deserialize)]
            struct Body {
                entity: String,
                slot: String,
                name: String,
            }
            let b: Body = from_body(body)?;
            db.use_prior(&b.entity, &b.slot, &b.name)?;
            Ok(json!({"ok": true}).to_string())
        }
        _ => Err(Error::Invalid(format!("no such route: {method} {path}"))),
    }
}

// ---------------------------------------------------------------- helpers

fn from_body<T: for<'de> Deserialize<'de>>(body: &str) -> Result<T> {
    serde_json::from_str(body).map_err(|e| Error::Invalid(format!("bad request body: {e}")))
}

fn req_param<'a>(params: &'a BTreeMap<String, String>, key: &str) -> Result<&'a str> {
    params
        .get(key)
        .map(|s| s.as_str())
        .ok_or_else(|| Error::Invalid(format!("missing param {key}")))
}

fn num_param(params: &BTreeMap<String, String>, key: &str) -> Result<Option<f64>> {
    match params.get(key) {
        None => Ok(None),
        Some(s) => s
            .parse::<f64>()
            .map(Some)
            .map_err(|_| Error::Invalid(format!("param {key} is not a number"))),
    }
}

fn at_param(params: &BTreeMap<String, String>) -> Result<i64> {
    match params.get("at") {
        Some(s) => parse_when(s),
        None => Ok(now_unix()),
    }
}

/// `at` in a JSON body: string date, numeric unix seconds, or absent (now).
fn at_value(v: Option<serde_json::Value>) -> Result<i64> {
    match v {
        None => Ok(now_unix()),
        Some(serde_json::Value::String(s)) => parse_when(&s),
        Some(serde_json::Value::Number(n)) => n
            .as_i64()
            .ok_or_else(|| Error::Invalid("'at' must be an integer or date string".into())),
        Some(_) => Err(Error::Invalid(
            "'at' must be an integer or date string".into(),
        )),
    }
}

fn predicate_from(params: &BTreeMap<String, String>) -> Result<Predicate> {
    let op = req_param(params, "op")?;
    let value_num = || {
        num_param(params, "value")?
            .ok_or_else(|| Error::Invalid("missing numeric param value".into()))
    };
    match op {
        "gt" => Ok(Predicate::Gt {
            value: value_num()?,
        }),
        "lt" => Ok(Predicate::Lt {
            value: value_num()?,
        }),
        "between" => {
            let lo = num_param(params, "lo")?
                .ok_or_else(|| Error::Invalid("missing param lo".into()))?;
            let hi = num_param(params, "hi")?
                .ok_or_else(|| Error::Invalid("missing param hi".into()))?;
            Ok(Predicate::Between { lo, hi })
        }
        "is" => Ok(Predicate::Is {
            value: req_param(params, "value")?.to_string(),
        }),
        "is_not" => Ok(Predicate::IsNot {
            value: req_param(params, "value")?.to_string(),
        }),
        _ => Err(Error::Invalid(format!("unknown predicate op {op:?}"))),
    }
}

fn parse_query(query: &str) -> BTreeMap<String, String> {
    let mut out = BTreeMap::new();
    for pair in query.split('&') {
        if pair.is_empty() {
            continue;
        }
        let (k, v) = pair.split_once('=').unwrap_or((pair, ""));
        out.insert(percent_decode(k), percent_decode(v));
    }
    out
}

fn percent_decode(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'+' => {
                out.push(b' ');
                i += 1;
            }
            b'%' if i + 2 < bytes.len() => {
                let hex = std::str::from_utf8(&bytes[i + 1..i + 3]).ok();
                match hex.and_then(|h| u8::from_str_radix(h, 16).ok()) {
                    Some(b) => {
                        out.push(b);
                        i += 3;
                    }
                    None => {
                        out.push(bytes[i]);
                        i += 1;
                    }
                }
            }
            b => {
                out.push(b);
                i += 1;
            }
        }
    }
    String::from_utf8_lossy(&out).into_owned()
}