ikigai-script 0.1.0

Scripts (Lisp; SPARQL queries with typed parameters and authority derived from the graphs they name; or plans, validated before they are stored, with authority derived from their steps) as resources in any ikigai host: `urn:script:{name}` is an atom with content-addressed versions, a compiled form, a result you read and runs you record, run under the least of what the script declares, what its publisher held at publish time, the host's ceiling and the runner's own capability. A host library: no binary, no elevation.
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
//! Draft privacy (ledger #952, part 1): a draft is visible only to its author until it is
//! published. To anyone else holding the script's read (or run) grant it is ABSENT, and
//! told so exactly as a name nobody wrote is, so a draft's existence leaks no more than its
//! text does.
//!
//! The author is the principal the HOST stamps (`SpaceConfig::principal`), the same value a
//! publish records as the script's publisher. These tests stamp it from a door that knows
//! who is calling independently of the capability, as a host's authenticated door does, so
//! two people holding the SAME capability are two principals: the case where a cached
//! answer could be served to the wrong one.

mod common;

use std::sync::{Arc, Mutex};

use common::*;
use ikigai_core::{Capability, Error, Kernel, Verb};
use ikigai_script::authority::{same_for_all, Ceiling, PrincipalStamper, ANONYMOUS};
use ikigai_script::{Catalog, MemoryBackend};

/// Who the door says is calling, set by each test before each call.
#[derive(Clone, Default)]
struct Door(Arc<Mutex<String>>);

impl Door {
    fn stamper(&self) -> PrincipalStamper {
        let who = Arc::clone(&self.0);
        Arc::new(move |_inv| who.lock().unwrap().clone())
    }

    fn call(
        &self,
        kernel: &Kernel,
        who: &str,
        capability: &Capability,
        verb: Verb,
        iri: &str,
        args: &[(&str, &str)],
    ) -> std::result::Result<String, Error> {
        *self.0.lock().unwrap() = who.to_string();
        call(kernel, capability, verb, iri, args)
    }
}

const ALICE: &str = "urn:test:person:alice";
const BOB: &str = "urn:test:person:bob";

/// What both people hold: the SAME capability, so only the door tells them apart.
fn editor(name: &str) -> Capability {
    cap(&[
        &format!("urn:cap:script:write:{name}"),
        &format!("urn:cap:script:read:{name}"),
        &format!("urn:cap:script:run:{name}"),
        "urn:cap:lisp",
    ])
}

fn stamped() -> (Host, Door) {
    let door = Door::default();
    let host = host_with(
        Arc::new(MemoryBackend::new()),
        same_for_all(Ceiling::unbounded()),
        Some(door.stamper()),
    );
    (host, door)
}

fn not_found(result: std::result::Result<String, Error>) -> String {
    match result {
        Err(Error::NotFound(message)) => message,
        other => panic!("expected a typed NotFound, got {other:?}"),
    }
}

#[test]
fn a_draft_is_visible_to_its_author_and_absent_to_every_other_reader() {
    let (host, door) = stamped();
    let k = &host.kernel;
    let who = editor("plan");
    door.call(
        k,
        ALICE,
        &who,
        Verb::Sink,
        "urn:script:plan",
        &[("content", "(+ 1 2)"), ("state", "draft")],
    )
    .unwrap();

    // The author reads it, twice (a cached answer, if there were one, would be hers).
    for _ in 0..2 {
        let source = door.call(k, ALICE, &who, Verb::Source, "urn:script:plan", &[]);
        assert_eq!(source.unwrap(), "(+ 1 2)");
        let exists = door.call(k, ALICE, &who, Verb::Exists, "urn:script:plan", &[]);
        assert_eq!(exists.unwrap(), "true\n");
    }
    // Bob holds the same capability and learns nothing, not even that it exists: exactly
    // what he is told about a name nobody wrote.
    let hidden = not_found(door.call(k, BOB, &who, Verb::Source, "urn:script:plan", &[]));
    let absent = not_found(door.call(
        k,
        BOB,
        &editor("nothing"),
        Verb::Source,
        "urn:script:nothing",
        &[],
    ));
    assert_eq!(hidden, absent.replace("nothing", "plan"));
    for _ in 0..2 {
        let exists = door.call(k, BOB, &who, Verb::Exists, "urn:script:plan", &[]);
        assert_eq!(exists.unwrap(), "false\n");
        let json = door.call(
            k,
            BOB,
            &who,
            Verb::Source,
            "urn:script:plan",
            &[("as", "application/json")],
        );
        not_found(json);
    }
    // And the author still sees it after Bob asked (no cached "absent" served to her).
    let exists = door.call(k, ALICE, &who, Verb::Exists, "urn:script:plan", &[]);
    assert_eq!(exists.unwrap(), "true\n");

    // Published, it is every reader's.
    door.call(
        k,
        ALICE,
        &who,
        Verb::Sink,
        "urn:script:plan",
        &[("content", "(+ 1 2)")],
    )
    .unwrap();
    let source = door.call(k, BOB, &who, Verb::Source, "urn:script:plan", &[]);
    assert_eq!(source.unwrap(), "(+ 1 2)");
}

#[test]
fn a_version_never_published_stays_its_authors_after_the_head_moves_on() {
    let (host, door) = stamped();
    let k = &host.kernel;
    let who = editor("notes");
    let published = door
        .call(
            k,
            ALICE,
            &who,
            Verb::Sink,
            "urn:script:notes",
            &[("content", "1")],
        )
        .unwrap();
    let draft = door
        .call(
            k,
            BOB,
            &who,
            Verb::Sink,
            "urn:script:notes",
            &[("content", "2"), ("state", "draft")],
        )
        .unwrap();
    // While the head is Bob's draft, Alice (who published the version before it) is not
    // its author.
    not_found(door.call(k, ALICE, &who, Verb::Source, "urn:script:notes", &[]));
    door.call(
        k,
        ALICE,
        &who,
        Verb::Sink,
        "urn:script:notes",
        &[("content", "3")],
    )
    .unwrap();

    let (published, draft) = (published.trim(), draft.trim());
    // The published version stays every reader's; the draft that was never published stays
    // Bob's, though the head has moved past it and its digest is in the history.
    for reader in [ALICE, BOB] {
        let v = door.call(k, reader, &who, Verb::Source, published, &[]);
        assert_eq!(v.unwrap(), "1");
    }
    assert_eq!(
        door.call(k, BOB, &who, Verb::Source, draft, &[]).unwrap(),
        "2"
    );
    assert_eq!(
        door.call(k, BOB, &who, Verb::Exists, draft, &[]).unwrap(),
        "true\n"
    );
    not_found(door.call(k, ALICE, &who, Verb::Source, draft, &[]));
    assert_eq!(
        door.call(k, ALICE, &who, Verb::Exists, draft, &[]).unwrap(),
        "false\n"
    );
}

#[test]
fn retiring_a_draft_does_not_publish_it() {
    let (host, door) = stamped();
    let k = &host.kernel;
    let who = cap(&[
        "urn:cap:script:write:wip",
        "urn:cap:script:read:wip",
        "urn:cap:script:delete:wip",
        "urn:cap:lisp",
    ]);
    door.call(
        k,
        ALICE,
        &who,
        Verb::Sink,
        "urn:script:wip",
        &[("content", "(+ 1 1)"), ("state", "draft")],
    )
    .unwrap();
    door.call(k, BOB, &who, Verb::Delete, "urn:script:wip", &[])
        .unwrap();
    not_found(door.call(k, BOB, &who, Verb::Source, "urn:script:wip", &[]));
    let record = door
        .call(
            k,
            ALICE,
            &who,
            Verb::Source,
            "urn:script:wip",
            &[("as", "application/json")],
        )
        .unwrap();
    assert!(record.contains("\"retired\""), "{record}");
}

#[test]
fn a_runner_who_is_not_the_author_cannot_read_a_drafts_compiled_form() {
    let (host, door) = stamped();
    let k = &host.kernel;
    let who = editor("job");
    door.call(
        k,
        ALICE,
        &who,
        Verb::Sink,
        "urn:script:job",
        &[("content", "(+ 2 2)"), ("state", "draft")],
    )
    .unwrap();
    let runner = cap(&["urn:cap:script:run:job", "urn:cap:lisp"]);
    // The compiled form carries the program: absent to Bob, the author's to Alice.
    not_found(door.call(
        k,
        BOB,
        &runner,
        Verb::Source,
        "urn:script:job:compiled",
        &[],
    ));
    door.call(
        k,
        ALICE,
        &runner,
        Verb::Source,
        "urn:script:job:compiled",
        &[],
    )
    .unwrap();
    // So a run is absent to Bob, and refused for its state to Alice; nothing evaluates.
    not_found(door.call(k, BOB, &runner, Verb::Source, "urn:script:job:result", &[]));
    not_found(door.call(k, BOB, &runner, Verb::Sink, "urn:script:job:runs", &[]));
    match door.call(
        k,
        ALICE,
        &runner,
        Verb::Source,
        "urn:script:job:result",
        &[],
    ) {
        Err(Error::Conflict(message)) => assert!(message.contains("draft"), "{message}"),
        other => panic!("expected Conflict, got {other:?}"),
    }
    assert_eq!(host.evals(), 0);
}

#[test]
fn the_catalog_lists_a_draft_to_its_author_alone() {
    let (host, door) = stamped();
    let k = &host.kernel;
    let both = cap(&[
        "urn:cap:script:write:a",
        "urn:cap:script:write:b",
        "urn:cap:script:read:a",
        "urn:cap:script:read:b",
        "urn:cap:lisp",
    ]);
    door.call(
        k,
        ALICE,
        &both,
        Verb::Sink,
        "urn:script:a",
        &[("content", "1")],
    )
    .unwrap();
    door.call(
        k,
        ALICE,
        &both,
        Verb::Sink,
        "urn:script:b",
        &[("content", "2"), ("state", "draft")],
    )
    .unwrap();
    let names = |who: &str| -> Vec<String> {
        let text = door
            .call(
                k,
                who,
                &both,
                Verb::Source,
                "urn:script:catalog",
                &[("as", "application/json")],
            )
            .unwrap();
        let catalog: Catalog = serde_json::from_str(&text).unwrap();
        catalog.scripts.into_iter().map(|e| e.name).collect()
    };
    assert_eq!(names(ALICE), ["a", "b"]);
    assert_eq!(names(BOB), ["a"]);
}

#[test]
fn an_unstamped_or_anonymous_principal_is_nobodys_author() {
    // A host that stamps nothing cannot tell its callers apart, so no caller can be shown to
    // be a draft's author: the draft is root's alone, and the refusal says why.
    let host = host();
    let k = &host.kernel;
    let who = editor("x");
    call(
        k,
        &who,
        Verb::Sink,
        "urn:script:x",
        &[("content", "1"), ("state", "draft")],
    )
    .unwrap();
    let message = not_found(call(k, &who, Verb::Source, "urn:script:x", &[]));
    assert!(message.contains("stamps no principal"), "{message}");
    assert_eq!(ok(k, Verb::Source, "urn:script:x", &[]), "1");

    // Two anonymous callers are not one person.
    let (host, door) = stamped();
    let k = &host.kernel;
    door.call(
        k,
        ANONYMOUS,
        &who,
        Verb::Sink,
        "urn:script:x",
        &[("content", "1"), ("state", "draft")],
    )
    .unwrap();
    not_found(door.call(k, ANONYMOUS, &who, Verb::Source, "urn:script:x", &[]));
}

#[test]
fn a_private_drafts_contract_says_nothing_its_text_does() {
    // Meta is answered from the description, to anyone who can reach the door, so a
    // SPARQL draft's own contract (its parameters, its comment) would publish its text. A
    // draft never published wears the template's contract instead.
    let door = Door::default();
    let host = sparql_host_with(
        ikigai_script::sparql::SparqlDoor::store(),
        same_for_all(Ceiling::unbounded()),
        Some(door.stamper()),
    );
    let k = &host.kernel;
    let query = "# The secret plan.\n# @param who xsd:string\n\
                 SELECT ?s FROM <urn:g:a> WHERE { ?s ?p ?who }";
    door.call(
        k,
        ALICE,
        &Capability::root(),
        Verb::Sink,
        "urn:script:q",
        &[
            ("content", query),
            ("language", "sparql"),
            ("state", "draft"),
        ],
    )
    .unwrap();
    let meta = |k: &Kernel| {
        call(
            k,
            &Capability::root(),
            Verb::Meta,
            "urn:script:q:result",
            &[],
        )
        .unwrap()
    };
    let contract = meta(k);
    assert!(!contract.contains("secret"), "{contract}");
    assert!(!contract.contains("\"who\""), "{contract}");
    assert_eq!(contract, meta_of_nothing(k));
    // Published, the query is its own entry again.
    door.call(
        k,
        ALICE,
        &Capability::root(),
        Verb::Sink,
        "urn:script:q",
        &[("content", query), ("language", "sparql")],
    )
    .unwrap();
    assert!(meta(k).contains("secret"));
}

fn meta_of_nothing(k: &Kernel) -> String {
    call(
        k,
        &Capability::root(),
        Verb::Meta,
        "urn:script:never:result",
        &[],
    )
    .unwrap()
}