demesne-postgres 9.0.0

Live-database support for demesne: schema introspection, policy-drift and coverage checks, and the RLS role audit.
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
use demesne_gocompat::json::{Value, marshal};
use postgres::error::SqlState;
use postgres::types::ToSql;
use postgres::{Error, Transaction};
use std::any::Any;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::panic::{self, AssertUnwindSafe};

const REACH_ROWS: &str = "demesne_reach_rows";
const REACH_AUTH: &str = "demesne_reach_auth";
const REACH_CALLER: &str = "demesne_reach_caller";
const NULL: Option<&str> = None;

type Claims = BTreeMap<String, String>;
type Arg<'a> = &'a (dyn ToSql + Sync);

struct T {
    name: String,
    errors: RefCell<Vec<String>>,
}

impl T {
    fn new(name: String) -> T {
        T { name, errors: RefCell::new(Vec::new()) }
    }

    fn errorf(&self, msg: impl Into<String>) {
        self.errors.borrow_mut().push(format!("{}: {}", self.name, msg.into()));
    }

    fn guarded(&self, f: impl FnOnce(&T)) {
        if let Err(p) = panic::catch_unwind(AssertUnwindSafe(|| f(self))) {
            self.errorf(format!("fatal: {}", panic_text(&*p)));
        }
    }

    fn run(&self, name: &str, f: impl FnOnce(&T)) {
        let sub = T::new(format!("{}/{}", self.name, name));
        sub.guarded(f);
        self.errors.borrow_mut().extend(sub.errors.take());
    }

    fn top(name: &str, f: impl FnOnce(&T)) {
        let t = T::new(name.to_string());
        t.guarded(f);
        let errors = t.errors.take();
        assert!(errors.is_empty(), "{} failure(s):\n{}", errors.len(), errors.join("\n"));
    }
}

fn panic_text(p: &(dyn Any + Send)) -> String {
    p.downcast_ref::<String>().cloned().or_else(|| p.downcast_ref::<&str>().map(std::string::ToString::to_string)).unwrap_or_default()
}

fn require_env(var: &str, why: &str) -> String {
    match std::env::var(var) {
        Ok(v) if !v.is_empty() => v,
        _ => panic!("set ${var} {why}"),
    }
}

fn claims(pairs: &[(&str, &str)]) -> Claims {
    pairs.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()
}

fn args<'a>(xs: &'a [&'a str]) -> Vec<Arg<'a>> {
    xs.iter().map(|x| x as Arg).collect()
}

#[derive(Clone, Copy)]
struct ReachTree {
    name: &'static str,
    root: &'static str,
    child: &'static str,
    other: &'static str,
    far: &'static str,
}

#[test]
#[ignore = "needs DEMESNE_PG_URL; run with -- --ignored"]
fn reach_patterns_postgres() {
    let url = require_env("DEMESNE_PG_URL", "to run the reach patterns against a live Postgres");
    T::top("reach_patterns_postgres", |t| {
        let src = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/../../examples/reach.demesne")).unwrap_or_else(|e| panic!("{e}"));
        let spec = demesne_syntax::parse(&format!("tables schema \"{REACH_ROWS}\"\ndefiners schema \"{REACH_AUTH}\"\n{src}")).unwrap_or_else(|e| panic!("{e}"));
        if let Err(e) = demesne_emit_sql::validate(&spec) {
            panic!("{e}");
        }
        for tree in [
            ReachTree { name: "ids opposite to depth", root: "z_root", child: "a_child", other: "m_other", far: "b_far" },
            ReachTree { name: "ids with depth", root: "a_root", child: "z_child", other: "m_other", far: "y_far" },
        ] {
            t.run(tree.name, |t| run_reach_patterns(t, &url, &spec, tree));
        }
    });
}

struct Db<'a, 'c> {
    t: &'a T,
    tx: &'a mut Transaction<'c>,
}

fn exec_on(tx: &mut Transaction<'_>, sql: &str, params: &[Arg]) -> Result<(), Error> {
    if params.is_empty() { tx.batch_execute(sql) } else { tx.execute(sql, params).map(|_| ()) }
}

impl<'c> Db<'_, 'c> {
    fn exec(&mut self, sql: &str, params: &[Arg]) {
        if let Err(e) = exec_on(self.tx, sql, params) {
            panic!("{e}\n{sql}");
        }
    }

    fn as_claims<R>(&mut self, claims: &Claims, f: impl FnOnce(&mut Transaction<'c>) -> Result<R, Error>) -> Result<R, Error> {
        let raw = marshal(&Value::Map(claims.iter().map(|(k, v)| (k.clone(), Value::str(v))).collect()));
        self.exec("SAVEPOINT reach_case", &[]);
        self.exec("SELECT set_config('request.jwt.claims', $1, true)", &[&raw]);
        self.exec(&format!("SET LOCAL ROLE {REACH_CALLER}"), &[]);
        let out = f(self.tx);
        self.exec("ROLLBACK TO SAVEPOINT reach_case", &[]);
        out
    }

    fn visible(&mut self, claims: &Claims, table: &str) -> Vec<String> {
        let sql = format!("SELECT id FROM {REACH_ROWS}.{table}");
        let got = self.as_claims(claims, |tx| tx.query(&sql, &[])?.iter().map(|r| r.try_get::<_, String>(0)).collect::<Result<Vec<_>, _>>());
        let mut ids = got.unwrap_or_else(|e| panic!("select {table} as {claims:?}: {e}"));
        ids.sort();
        ids
    }

    fn updated(&mut self, claims: &Claims, table: &str, id: &str) -> u64 {
        let sql = format!("UPDATE {REACH_ROWS}.{table} SET id = id WHERE id = $1");
        self.as_claims(claims, |tx| tx.execute(&sql, &[&id])).unwrap_or_else(|e| panic!("update {table} {id} as {claims:?}: {e}"))
    }

    fn truth(&mut self, claims: &Claims, sql: &str, params: &[Arg]) -> bool {
        let got = self.as_claims(claims, |tx| tx.query_one(sql, params)?.try_get::<_, Option<bool>>(0)).unwrap_or_else(|e| panic!("{sql} as {claims:?}: {e}"));
        got == Some(true)
    }

    fn insert_refused(&mut self, claims: &Claims, sql: &str, params: &[Arg]) -> bool {
        match self.as_claims(claims, |tx| exec_on(tx, sql, params)) {
            Err(e) if e.code() == Some(&SqlState::INSUFFICIENT_PRIVILEGE) => true,
            Err(e) => panic!("{sql} as {claims:?}: {e}"),
            Ok(()) => false,
        }
    }
}

fn want_ids(t: &T, what: &str, got: &[String], want: &[&str]) {
    let mut want: Vec<&str> = want.to_vec();
    want.sort_unstable();
    if got.join(",") != want.join(",") {
        t.errorf(format!("{what}: got {got:?}, want {want:?}"));
    }
}

fn run_reach_patterns(t: &T, url: &str, spec: &demesne_syntax::Spec, tree: ReachTree) {
    let mut conn = demesne_postgres::connect(url).unwrap_or_else(|e| panic!("{e}"));
    let mut tx = conn.transaction().unwrap_or_else(|e| panic!("{e}"));
    install_reach_schema(&mut Db { t, tx: &mut tx }, spec);
    seed_reach_rows(&mut Db { t, tx: &mut tx }, &tree);

    t.run("scope ladder decides from the session", |t| check_ladder(&mut Db { t, tx: &mut tx }, &tree));
    t.run("each level's check agrees with the set it shares a body with", |t| check_in_levels_against_the_set(&mut Db { t, tx: &mut tx }, &tree));
    t.run("selections", |t| check_selections(&mut Db { t, tx: &mut tx }, &tree));
    t.run("edge direction per object", |t| check_direction(&mut Db { t, tx: &mut tx }, &tree));
    t.run("claim lifts reads only", |t| check_claim_lift(&mut Db { t, tx: &mut tx }, &tree));
    t.run("borrow compiles for its op", |t| check_borrow(&mut Db { t, tx: &mut tx }, &tree));
    t.run("admit arm and export", |t| check_admit(&mut Db { t, tx: &mut tx }, &tree));
    let _ = tx.rollback();
}

fn install_reach_schema(d: &mut Db, spec: &demesne_syntax::Spec) {
    d.exec(&format!("CREATE SCHEMA {REACH_ROWS}; CREATE SCHEMA {REACH_AUTH}; CREATE ROLE {REACH_CALLER} NOLOGIN"), &[]);
    for table in [
        "approvals(holder_id text, estate_id text, space_id text, folder_id text, ended_at timestamptz, expires_at timestamptz)",
        "paths(ancestor_id text, descendant_id text)",
        "assets(id text, estate_id text, space_id text, folder_id text)",
        "blueprints(id text, estate_id text, space_id text, folder_id text)",
        "ledgers(id text, estate_id text)",
        "allowances(id text, estate_id text, space_id text)",
        "spaces(id text, estate_id text)",
        "catalogs(id text, estate_id text, space_id text, folder_id text)",
        "entries(id text, estate_id text, space_id text, catalog_id text)",
        "folders(id text, estate_id text, space_id text, parent_id text)",
    ] {
        d.exec(&format!("CREATE TABLE {REACH_ROWS}.{table}"), &[]);
    }
    let sql = demesne_emit_sql::Sql::new(spec);
    let defs = sql.emit_definers().unwrap_or_else(|e| panic!("{e}"));
    d.exec("SET LOCAL check_function_bodies = off", &[]);
    for def in &defs {
        d.exec(&def.create_sql(), &[]);
    }
    let rls = match sql.emit_rls() {
        Ok(r) if r.unsupported.is_empty() => r,
        Ok(r) => panic!("emit rls: <nil> {:?}", r.unsupported),
        Err(e) => panic!("emit rls: {e} []"),
    };
    d.exec(&rls.enablement_sql(), &[]);
    d.exec(&rls.policy_sql(REACH_CALLER), &[]);
    d.exec(&format!("GRANT USAGE ON SCHEMA {REACH_ROWS}, {REACH_AUTH} TO {REACH_CALLER}"), &[]);
    d.exec(&format!("GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA {REACH_ROWS} TO {REACH_CALLER}"), &[]);
    d.exec(
        &format!(
            "GRANT EXECUTE ON FUNCTION {REACH_AUTH}.approvals_reach_in_space(text, text, text), {REACH_AUTH}.approvals_reach_in_folder(text, text, text, text) TO {REACH_CALLER}"
        ),
        &[],
    );
}

fn seed_reach_rows(d: &mut Db, tr: &ReachTree) {
    for (id, space, parent) in [(tr.root, "s1", NULL), (tr.child, "s1", Some(tr.root)), (tr.other, "s1", NULL), (tr.far, "s2", NULL)] {
        d.exec(&format!("INSERT INTO {REACH_ROWS}.folders VALUES ($1, 'e1', $2, $3)"), &[&id, &space, &parent]);
        d.exec(&format!("INSERT INTO {REACH_ROWS}.paths VALUES ($1, $1)"), &[&id]);
    }
    d.exec(&format!("INSERT INTO {REACH_ROWS}.paths VALUES ($1, $2)"), &[&tr.root, &tr.child]);
    for (holder, space, folder, ended) in
        [("h_estate", NULL, NULL, false), ("h_space", Some("s1"), NULL, false), ("h_folder", Some("s1"), Some(tr.root), false), ("h_ended", NULL, NULL, true)]
    {
        let ended = if ended { "now() - interval '1 minute'" } else { "NULL" };
        d.exec(&format!("INSERT INTO {REACH_ROWS}.approvals VALUES ($1, 'e1', $2, $3, {ended}, now() + interval '1 day')"), &[&holder, &space, &folder]);
    }
    for table in ["assets", "blueprints"] {
        for (id, space, folder) in [
            (tr.root, "s1", Some(tr.root)),
            (tr.child, "s1", Some(tr.child)),
            (tr.other, "s1", Some(tr.other)),
            ("shared", "s1", NULL),
            (tr.far, "s2", Some(tr.far)),
        ] {
            d.exec(&format!("INSERT INTO {REACH_ROWS}.{table} VALUES ($1, 'e1', $2, $3)"), &[&id, &space, &folder]);
        }
    }
    d.exec(&format!("INSERT INTO {REACH_ROWS}.ledgers VALUES ('l1', 'e1')"), &[]);
    d.exec(&format!("INSERT INTO {REACH_ROWS}.allowances VALUES ('al_s1', 'e1', 's1'), ('al_s2', 'e1', 's2')"), &[]);
    d.exec(&format!("INSERT INTO {REACH_ROWS}.spaces VALUES ('s1', 'e1'), ('s2', 'e1')"), &[]);
    d.exec(&format!("INSERT INTO {REACH_ROWS}.catalogs VALUES ($1, 'e1', 's1', $1), ($2, 'e1', 's1', $2)"), &[&tr.root, &tr.child]);
    d.exec(&format!("INSERT INTO {REACH_ROWS}.entries VALUES ($1, 'e1', 's1', $1), ($2, 'e1', 's1', $2)"), &[&tr.root, &tr.child]);
}

fn check_ladder(d: &mut Db, tr: &ReachTree) {
    let t = d.t;
    let v = d.visible(&claims(&[("sub", "h_estate"), ("space_id", "s1")]), "assets");
    want_ids(t, "estate-wide helper without a folder claim", &v, &["shared"]);
    let v = d.visible(&claims(&[("sub", "h_estate"), ("space_id", "s1"), ("folder_id", tr.root)]), "assets");
    want_ids(t, "estate-wide helper standing in the root", &v, &[tr.root, tr.child, "shared"]);
    let v = d.visible(&claims(&[("sub", "h_space"), ("space_id", "s1"), ("folder_id", tr.root)]), "assets");
    want_ids(t, "space-pinned helper in its space", &v, &[tr.root, tr.child, "shared"]);
    let v = d.visible(&claims(&[("sub", "h_space"), ("space_id", "s2"), ("folder_id", tr.far)]), "assets");
    want_ids(t, "space-pinned helper in another space", &v, &[]);
    let v = d.visible(&claims(&[("sub", "h_folder"), ("space_id", "s1"), ("folder_id", tr.root)]), "assets");
    want_ids(t, "folder-pinned helper in its folder", &v, &[tr.root, tr.child, "shared"]);
    let v = d.visible(&claims(&[("sub", "h_folder"), ("space_id", "s1"), ("folder_id", tr.other)]), "assets");
    want_ids(t, "folder-pinned helper beside its folder", &v, &[]);
    let v = d.visible(&claims(&[("sub", "h_folder"), ("space_id", "s1")]), "assets");
    want_ids(t, "folder-pinned helper standing in the space", &v, &["shared"]);
    let v = d.visible(&claims(&[("sub", "h_ended"), ("space_id", "s1"), ("folder_id", tr.root)]), "assets");
    want_ids(t, "ended approval", &v, &[]);

    let probe = format!("SELECT {REACH_AUTH}.approvals_reach_in_space($1, 'e1', '')");
    d.exec("SAVEPOINT empty_claims", &[]);
    d.exec("SELECT set_config('request.jwt.claims', '', true)", &[]);
    match d.tx.query_one(&probe, &[&"h_space"]).and_then(|r| r.try_get::<_, Option<bool>>(0)) {
        Ok(None | Some(false)) => {}
        Ok(Some(true)) => t.errorf("a direct call with an empty claims setting is nobody's own, so it must not answer true"),
        Err(e) => t.errorf(format!("a direct call with an empty claims setting must answer rather than raise: {e}")),
    }
    d.exec("ROLLBACK TO SAVEPOINT empty_claims", &[]);

    let in_space = format!("SELECT {REACH_AUTH}.approvals_reach_in_space($1, 'e1', $2)");
    let in_folder = format!("SELECT {REACH_AUTH}.approvals_reach_in_folder($1, 'e1', $2, $3)");
    for (sql, a, want) in [
        (&in_space, vec!["h_space", ""], true),
        (&in_space, vec!["h_space", "s2"], false),
        (&in_space, vec!["h_space", "s1"], true),
        (&in_folder, vec!["h_folder", "s1", ""], true),
        (&in_folder, vec!["h_folder", "s1", tr.other], false),
        (&in_folder, vec!["h_folder", "s1", tr.root], true),
        (&in_space, vec!["h_folder", "s2"], false),
    ] {
        let got = d.truth(&claims(&[("sub", a[0])]), sql, &args(&a));
        if got != want {
            t.errorf(format!("{sql} {a:?} = {got}, want {want}"));
        }
        if d.truth(&claims(&[("sub", "h_ended")]), sql, &args(&a)) || d.truth(&claims(&[]), sql, &args(&a)) {
            t.errorf(format!("{sql} {a:?} answered a caller who is not {}", a[0]));
        }
    }
}

fn check_in_levels_against_the_set(d: &mut Db, tr: &ReachTree) {
    let t = d.t;
    let set = format!("SELECT $1 IN (SELECT {REACH_AUTH}.approvals_reach_set($2))");
    let in_space = format!("SELECT {REACH_AUTH}.approvals_reach_in_space($2, $1, $3)");
    let in_folder = format!("SELECT {REACH_AUTH}.approvals_reach_in_folder($2, $1, $3, $4)");
    let people = ["h_estate", "h_space", "h_folder", "h_ended", "h_none"];
    let folders = [None, Some(tr.root), Some(tr.child), Some(tr.other), Some(tr.far)];
    let cases = people
        .iter()
        .flat_map(|&caller| people.iter().map(move |&holder| (caller, holder)))
        .flat_map(|(caller, holder)| ["e1", "e2"].into_iter().map(move |estate| (caller, holder, estate)))
        .flat_map(|(caller, holder, estate)| ["s1", "s2"].into_iter().map(move |space| (caller, holder, estate, space)))
        .flat_map(|(caller, holder, estate, space)| folders.into_iter().map(move |folder| (caller, holder, estate, space, folder)));
    let mut outcomes = [0usize; 2];
    for (caller, holder, estate, space, folder) in cases {
        let (by_level, by_set) = match folder {
            None => (
                d.truth(&claims(&[("sub", caller)]), &in_space, &args(&[estate, holder, space])),
                d.truth(&claims(&[("sub", caller), ("space_id", space)]), &set, &args(&[estate, holder])),
            ),
            Some(f) => (
                d.truth(&claims(&[("sub", caller)]), &in_folder, &args(&[estate, holder, space, f])),
                d.truth(&claims(&[("sub", caller), ("space_id", space), ("folder_id", f)]), &set, &args(&[estate, holder])),
            ),
        };
        if by_level != by_set {
            t.errorf(format!("{caller} asks about {holder} at {estate}/{space}/{folder:?}: the level's check says {by_level}, the set says {by_set}"));
        }
        outcomes[usize::from(by_level)] += 1;
    }
    if outcomes[0] == 0 || outcomes[1] == 0 {
        t.errorf(format!("the grid must both admit and refuse: {outcomes:?}"));
    }
    let empty_level = d.truth(&claims(&[("sub", "h_space")]), &in_space, &args(&["e1", "h_space", ""]));
    let empty_set = d.truth(&claims(&[("sub", "h_space")]), &set, &args(&["e1", "h_space"]));
    if !empty_level || empty_set {
        t.errorf(format!("an empty selection admits a pinned approval in the level's check ({empty_level}) and not in the set ({empty_set})"));
    }
}

fn check_selections(d: &mut Db, tr: &ReachTree) {
    let t = d.t;
    let v = d.visible(&claims(&[("sub", "h_estate")]), "ledgers");
    want_ids(t, "unscoped: estate-wide helper", &v, &["l1"]);
    let v = d.visible(&claims(&[("sub", "h_space"), ("space_id", "s1")]), "ledgers");
    want_ids(t, "unscoped: space-pinned helper", &v, &[]);
    let v = d.visible(&claims(&[("sub", "h_folder"), ("space_id", "s1"), ("folder_id", tr.root)]), "ledgers");
    want_ids(t, "unscoped: folder-pinned helper", &v, &[]);
    let v = d.visible(&claims(&[("sub", "h_estate")]), "spaces");
    want_ids(t, "bound: estate-wide helper", &v, &["s1", "s2"]);
    let v = d.visible(&claims(&[("sub", "h_space"), ("space_id", "s1")]), "spaces");
    want_ids(t, "bound: space-pinned helper in its space", &v, &["s1"]);
    let v = d.visible(&claims(&[("sub", "h_space"), ("space_id", "s2")]), "spaces");
    want_ids(t, "bound: space-pinned helper claiming another space", &v, &[]);
    let v = d.visible(&claims(&[("sub", "h_folder"), ("space_id", "s1"), ("folder_id", tr.root)]), "spaces");
    want_ids(t, "bound: folder-pinned helper", &v, &["s1"]);
    check_bound_column(d, tr);
    let n = d.updated(&claims(&[("sub", "h_space"), ("space_id", "s1")]), "spaces", "s1");
    if n != 0 {
        t.errorf(format!("a space-pinned helper updated its own space row ({n} rows); update takes the unscoped reach"));
    }
    let n = d.updated(&claims(&[("sub", "h_estate"), ("space_id", "s1")]), "spaces", "s1");
    if n != 1 {
        t.errorf(format!("an estate-wide helper could not update the space row ({n} rows)"));
    }
}

fn check_direction(d: &mut Db, tr: &ReachTree) {
    let t = d.t;
    let at = |folder: &str| claims(&[("estate_id", "e1"), ("space_id", "s1"), ("folder_id", folder)]);
    let v = d.visible(&at(tr.root), "assets");
    want_ids(t, "rows reach down from the root", &v, &[tr.root, tr.child, "shared"]);
    let v = d.visible(&at(tr.child), "assets");
    want_ids(t, "rows do not reach up from the child", &v, &[tr.child, "shared"]);
    let v = d.visible(&at(tr.child), "blueprints");
    want_ids(t, "definitions inherit up to the child", &v, &[tr.root, tr.child, "shared"]);
    let v = d.visible(&at(tr.root), "blueprints");
    want_ids(t, "definitions do not reach down from the root", &v, &[tr.root, "shared"]);
    let v = d.visible(&at(tr.other), "blueprints");
    want_ids(t, "a sibling inherits nothing", &v, &[tr.other, "shared"]);
    let n = d.updated(&at(tr.child), "blueprints", tr.root);
    if n != 0 {
        t.errorf(format!("the child updated an inherited definition ({n} rows)"));
    }
    let n = d.updated(&at(tr.root), "assets", tr.child);
    if n != 0 {
        t.errorf(format!("the root updated a descendant row through a read-only reach ({n} rows)"));
    }
}

fn check_claim_lift(d: &mut Db, tr: &ReachTree) {
    let t = d.t;
    let lifted = claims(&[("estate_id", "e1"), ("space_id", "s1"), ("folder_id", tr.other), ("view_mode", "all")]);
    let v = d.visible(&lifted, "assets");
    want_ids(t, "lifted read", &v, &[tr.root, tr.child, tr.other, "shared"]);
    let v = d.visible(&claims(&[("estate_id", "e1"), ("space_id", "s1"), ("folder_id", tr.other), ("view_mode", "some")]), "assets");
    want_ids(t, "other claim value", &v, &[tr.other, "shared"]);
    let v = d.visible(&claims(&[("estate_id", "e1"), ("space_id", "s2"), ("view_mode", "all")]), "assets");
    want_ids(t, "the lift stays inside the space", &v, &[tr.far]);
    let n = d.updated(&lifted, "assets", tr.root);
    if n != 0 {
        t.errorf(format!("the lifted claim updated a row outside its folder ({n} rows)"));
    }
    let n = d.updated(&lifted, "assets", tr.other);
    if n != 1 {
        t.errorf(format!("the lifted claim could not update its own folder's row ({n} rows)"));
    }
}

fn check_borrow(d: &mut Db, tr: &ReachTree) {
    let t = d.t;
    let staff = |folder: &str| claims(&[("estate_id", "e1"), ("space_id", "s1"), ("folder_id", folder), ("kind", "staff")]);
    let v = d.visible(&staff(tr.child), "entries");
    want_ids(t, "read borrow inherits up", &v, &[tr.root, tr.child]);
    let v = d.visible(&staff(tr.root), "entries");
    want_ids(t, "read borrow does not reach down", &v, &[tr.root]);
    let n = d.updated(&staff(tr.child), "entries", tr.root);
    if n != 0 {
        t.errorf(format!("write borrow admitted an inherited catalog ({n} rows)"));
    }
    let n = d.updated(&staff(tr.child), "entries", tr.child);
    if n != 1 {
        t.errorf(format!("write borrow refused the caller's own catalog ({n} rows)"));
    }
    let mut robot = staff(tr.child);
    robot.insert("kind".into(), "robot".into());
    let v = d.visible(&robot, "entries");
    want_ids(t, "borrowed authority still applies", &v, &[]);
}

fn check_admit(d: &mut Db, tr: &ReachTree) {
    let t = d.t;
    let robot = |folder: &str| {
        let mut c = claims(&[("estate_id", "e1"), ("space_id", "s1"), ("kind", "robot")]);
        if !folder.is_empty() {
            c.insert("folder_id".into(), folder.into());
        }
        c
    };
    let staff = claims(&[("estate_id", "e1"), ("space_id", "s1"), ("kind", "staff")]);
    let v = d.visible(&robot(tr.root), "folders");
    want_ids(t, "robot confined to the root", &v, &[tr.root, tr.child]);
    let v = d.visible(&robot(tr.child), "folders");
    want_ids(t, "robot confined to the child", &v, &[tr.child]);
    let v = d.visible(&robot(""), "folders");
    want_ids(t, "unconfined robot", &v, &[tr.root, tr.child, tr.other]);
    let v = d.visible(&staff, "folders");
    want_ids(t, "staff through the permission", &v, &[tr.root, tr.child, tr.other]);
    let v = d.visible(&claims(&[("estate_id", "e1"), ("space_id", "s2"), ("kind", "robot"), ("folder_id", tr.root)]), "folders");
    want_ids(t, "robot in another space", &v, &[]);

    let insert = format!("INSERT INTO {REACH_ROWS}.folders VALUES ('fresh', 'e1', 's1', $1)");
    if d.insert_refused(&robot(tr.root), &insert, &[&tr.root]) {
        t.errorf("a robot confined to the root could not create a folder beneath it");
    }
    if !d.insert_refused(&robot(tr.root), &insert, &[&tr.other]) {
        t.errorf("a robot confined to the root created a folder under a sibling");
    }
    if !d.insert_refused(&robot(tr.root), &insert, &[&NULL]) {
        t.errorf("a confined robot created a top-level folder");
    }
    if d.insert_refused(&robot(""), &insert, &[&NULL]) {
        t.errorf("an unconfined robot could not create a top-level folder");
    }

    let allowed = format!("SELECT {REACH_AUTH}.folder_write_allowed($1, 'e1', $2)");
    for c in [robot(tr.root), robot(tr.child), robot(""), staff.clone(), claims(&[("estate_id", "e1"), ("space_id", "s1"), ("kind", "visitor")])] {
        for id in [tr.root, tr.child, tr.other, tr.far] {
            let space = if id == tr.far { "s2" } else { "s1" };
            let exported = d.truth(&c, &allowed, &[&id, &space]);
            let policy = d.updated(&c, "folders", id) == 1;
            if exported != policy {
                t.errorf(format!("folder_write_allowed({id}) = {exported} but the update policy admitted it = {policy}, as {c:?}"));
            }
        }
    }
    let allowed_s1 = format!("SELECT {REACH_AUTH}.folder_write_allowed($1, 'e1', 's1')");
    if !d.truth(&robot(tr.root), &allowed_s1, &[&tr.child]) {
        t.errorf("export: a confined robot may write its descendant");
    }
    if d.truth(&robot(tr.root), &allowed_s1, &[&tr.other]) {
        t.errorf("export: a confined robot may not write a sibling");
    }
}

fn check_bound_column(d: &mut Db, tr: &ReachTree) {
    let t = d.t;
    let v = d.visible(&claims(&[("sub", "h_space"), ("space_id", "s1")]), "allowances");
    want_ids(t, "bound column: space-pinned helper reads its own space's row", &v, &["al_s1"]);
    let v = d.visible(&claims(&[("sub", "h_space"), ("space_id", "s2")]), "allowances");
    want_ids(t, "bound column: space-pinned helper claiming another space", &v, &[]);
    let v = d.visible(&claims(&[("sub", "h_folder"), ("space_id", "s1"), ("folder_id", tr.root)]), "allowances");
    want_ids(t, "bound column: folder-pinned helper", &v, &["al_s1"]);
    let v = d.visible(&claims(&[("sub", "h_estate"), ("space_id", "s1")]), "allowances");
    want_ids(t, "bound column: estate-wide helper", &v, &["al_s1", "al_s2"]);
    for id in ["al_s1", "al_s2"] {
        let n = d.updated(&claims(&[("sub", "h_space"), ("space_id", "s1")]), "allowances", id);
        if n != 0 {
            t.errorf(format!("a space-pinned helper updated allowance {id} ({n} rows); writes take the unscoped reach"));
        }
        let n = d.updated(&claims(&[("sub", "h_estate"), ("space_id", "s1")]), "allowances", id);
        if n != 1 {
            t.errorf(format!("an estate-wide helper could not update allowance {id} ({n} rows)"));
        }
    }
    let insert = format!("INSERT INTO {REACH_ROWS}.allowances VALUES ('al_new', 'e1', 's1')");
    if !d.insert_refused(&claims(&[("sub", "h_space"), ("space_id", "s1")]), &insert, &[]) {
        t.errorf("a space-pinned helper inserted an allowance");
    }
    if d.insert_refused(&claims(&[("sub", "h_estate"), ("space_id", "s1")]), &insert, &[]) {
        t.errorf("an estate-wide helper could not insert an allowance");
    }
}