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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! The life of a script: publish, fetch without running, run as a read (and its caching),
//! run for effects (and the run record), versions, retire.

mod common;

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

use common::*;
use ikigai_core::{Capability, Error, Verb};
use ikigai_script::authority::{Ceiling, CeilingPolicy};
use ikigai_script::{Catalog, MemoryBackend, Run};

#[test]
fn publish_then_fetch_without_running() {
    let host = host();
    let version = publish(&host.kernel, "hello", "(+ 1 2)", &[]);
    assert!(
        version.starts_with("urn:script:hello:version:sha256:"),
        "{version}"
    );
    // The source, verbatim — and nothing ran.
    let source = ok(&host.kernel, Verb::Source, "urn:script:hello", &[]);
    assert_eq!(source, "(+ 1 2)");
    let record = ok(
        &host.kernel,
        Verb::Source,
        "urn:script:hello",
        &[("as", "application/json")],
    );
    let record: serde_json::Value = serde_json::from_str(&record).unwrap();
    assert_eq!(record["state"], "published");
    assert_eq!(record["public"], false);
    assert_eq!(record["language"], "lisp");
    assert_eq!(record["versionIri"], version.as_str());
    // The language's capability is declared implicitly.
    assert_eq!(record["requires"], serde_json::json!(["urn:cap:lisp"]));
    assert_eq!(record["publisher"], "urn:script:principal:unstamped");
    assert_eq!(host.evals(), 0, "fetching a script must not run it");
    assert_eq!(
        ok(&host.kernel, Verb::Exists, "urn:script:hello", &[]),
        "true\n"
    );
    assert_eq!(
        ok(&host.kernel, Verb::Exists, "urn:script:nobody", &[]),
        "false\n"
    );
}

#[test]
fn a_result_is_the_scripts_answer_and_runs_each_time_unless_it_opts_in() {
    let host = host();
    publish(&host.kernel, "live", "(+ 1 2)", &[]);
    assert_eq!(
        ok(&host.kernel, Verb::Source, "urn:script:live:result", &[]),
        "3"
    );
    assert_eq!(
        ok(&host.kernel, Verb::Source, "urn:script:live:result", &[]),
        "3"
    );
    assert_eq!(host.evals(), 2, "an uncacheable program runs on every read");

    publish(&host.kernel, "pure", "(cacheable (* 6 7))", &[]);
    let before = host.evals();
    assert_eq!(
        ok(&host.kernel, Verb::Source, "urn:script:pure:result", &[]),
        "42"
    );
    assert_eq!(
        ok(&host.kernel, Verb::Source, "urn:script:pure:result", &[]),
        "42"
    );
    assert_eq!(
        host.evals() - before,
        1,
        "a program that opts in is served from the cache the second time"
    );

    // Republishing cuts the script's thread, so the cached answer is not served again.
    publish(&host.kernel, "pure", "(cacheable (* 6 8))", &[]);
    assert_eq!(
        ok(&host.kernel, Verb::Source, "urn:script:pure:result", &[]),
        "48"
    );
    assert_eq!(host.evals() - before, 2);
}

#[test]
fn the_host_cuts_the_authority_thread_when_it_changes_a_ceiling() {
    let ceiling = Arc::new(Mutex::new(Ceiling::unbounded()));
    let policy: CeilingPolicy = {
        let ceiling = Arc::clone(&ceiling);
        Arc::new(move |_| ceiling.lock().unwrap().clone())
    };
    let host = host_with(Arc::new(MemoryBackend::new()), policy, None);
    publish(
        &host.kernel,
        "whoami",
        "(cacheable (source \"urn:test:whoami\"))",
        &[("requires", CAP_VAULT)],
    );
    let read = || ok(&host.kernel, Verb::Source, "urn:script:whoami:result", &[]);
    assert_eq!(read(), format!("urn:cap:lisp\n{CAP_VAULT}"));

    // The host narrows the ceiling. The library cannot see the host's file change, so a
    // cached answer computed under the old ceiling is still served…
    *ceiling.lock().unwrap() = Ceiling::scoped(["urn:cap:lisp"]);
    assert_eq!(read(), format!("urn:cap:lisp\n{CAP_VAULT}"));
    // …until the host cuts the script's authority thread, which is its half of the contract.
    host.kernel.cut("urn:script:whoami:authority");
    assert_eq!(read(), "urn:cap:lisp");
}

#[test]
fn data_reaches_the_script_as_input_never_as_code() {
    let host = host();
    publish(
        &host.kernel,
        "echo",
        "(string-append \"got \" (input))",
        &[],
    );
    assert_eq!(
        ok(
            &host.kernel,
            Verb::Source,
            "urn:script:echo:result",
            &[("data", "(+ 1 2)")]
        ),
        "got (+ 1 2)"
    );
}

#[test]
fn a_run_for_effects_is_recorded() {
    let host = host();
    let version = publish(
        &host.kernel,
        "stash",
        "(sink \"urn:test:vault\" (input))",
        &[("requires", CAP_VAULT)],
    );
    let run = ok(
        &host.kernel,
        Verb::Sink,
        "urn:script:stash:runs",
        &[("content", "the payload")],
    );
    assert_eq!(run.trim(), "urn:script:stash:run:1");
    let record = ok(
        &host.kernel,
        Verb::Source,
        "urn:script:stash:run:1",
        &[("as", "application/json")],
    );
    let record: Run = serde_json::from_str(&record).unwrap();
    assert_eq!(record.id, 1);
    assert_eq!(
        Some(record.version.as_str()),
        version.rsplit(":version:").next()
    );
    assert_eq!(record.outcome, ikigai_script::Outcome::Ok);
    assert_eq!(record.result.as_deref(), Some("stored"));
    assert!(record.started.is_some() && record.ended > record.started);
    // Under root the run holds exactly what the script declared, and nothing else.
    assert_eq!(
        record
            .capability
            .iter()
            .map(String::as_str)
            .collect::<Vec<_>>(),
        vec!["urn:cap:lisp", CAP_VAULT]
    );
    let text = ok(&host.kernel, Verb::Source, "urn:script:stash:run:1", &[]);
    assert!(text.contains("outcome:    ok"), "{text}");
    // A second run is the next number.
    let run = ok(
        &host.kernel,
        Verb::Sink,
        "urn:script:stash:runs",
        &[("content", "x")],
    );
    assert_eq!(run.trim(), "urn:script:stash:run:2");
    assert_eq!(
        ok(&host.kernel, Verb::Exists, "urn:script:stash:run:2", &[]),
        "true\n"
    );
    assert_eq!(
        ok(&host.kernel, Verb::Exists, "urn:script:stash:run:3", &[]),
        "false\n"
    );
}

#[test]
fn a_failed_run_is_recorded_and_its_error_keeps_its_type() {
    let host = host();
    // Declares nothing beyond Lisp, so the vault refuses it.
    publish(
        &host.kernel,
        "overreach",
        "(sink \"urn:test:vault\" \"x\")",
        &[],
    );
    let message = denied(call(
        &host.kernel,
        &Capability::root(),
        Verb::Sink,
        "urn:script:overreach:runs",
        &[],
    ));
    assert!(
        message.contains("run recorded at urn:script:overreach:run:1"),
        "{message}"
    );
    let record: Run = serde_json::from_str(&ok(
        &host.kernel,
        Verb::Source,
        "urn:script:overreach:run:1",
        &[("as", "application/json")],
    ))
    .unwrap();
    assert!(
        matches!(&record.outcome, ikigai_script::Outcome::Failed { kind, .. } if kind == "denied"),
        "{record:?}"
    );
}

#[test]
fn republishing_makes_a_new_version_and_the_old_one_stays() {
    let host = host();
    let first = publish(&host.kernel, "evolving", "1", &[]);
    let second = publish(&host.kernel, "evolving", "2", &[]);
    assert_ne!(first, second);
    // The head moved.
    assert_eq!(
        ok(
            &host.kernel,
            Verb::Source,
            "urn:script:evolving:result",
            &[]
        ),
        "2"
    );
    // Both versions are fetchable, by their own names.
    assert_eq!(ok(&host.kernel, Verb::Source, &first, &[]), "1");
    assert_eq!(ok(&host.kernel, Verb::Source, &second, &[]), "2");
    assert_eq!(ok(&host.kernel, Verb::Exists, &first, &[]), "true\n");
    // The same content is the same version: content-addressed.
    assert_eq!(publish(&host.kernel, "evolving", "1", &[]), first);
    // The history keeps every change.
    let record: serde_json::Value = serde_json::from_str(&ok(
        &host.kernel,
        Verb::Source,
        "urn:script:evolving",
        &[("as", "application/json")],
    ))
    .unwrap();
    assert_eq!(record["history"].as_array().unwrap().len(), 3);
    // A version of another content is not found.
    let missing = format!("urn:script:evolving:version:sha256:{}", "0".repeat(64));
    assert!(matches!(
        call(
            &host.kernel,
            &Capability::root(),
            Verb::Source,
            &missing,
            &[]
        ),
        Err(Error::NotFound(_))
    ));
}

#[test]
fn if_version_refuses_a_write_that_lost_a_race() {
    let host = host();
    let first = publish(&host.kernel, "raced", "1", &[("if-version", "none")]);
    let digest = first.rsplit(":version:").next().unwrap().to_string();
    publish(&host.kernel, "raced", "2", &[]);
    let lost = call(
        &host.kernel,
        &Capability::root(),
        Verb::Sink,
        "urn:script:raced",
        &[("content", "3"), ("if-version", &digest)],
    );
    assert!(matches!(lost, Err(Error::Conflict(_))), "{lost:?}");
    assert_eq!(ok(&host.kernel, Verb::Source, "urn:script:raced", &[]), "2");
}

#[test]
fn a_retired_script_is_readable_and_never_runs() {
    let host = host();
    let version = publish(&host.kernel, "old", "1", &[]);
    assert_eq!(
        ok(&host.kernel, Verb::Source, "urn:script:old:result", &[]),
        "1"
    );
    assert_eq!(
        ok(&host.kernel, Verb::Delete, "urn:script:old", &[]),
        "retired urn:script:old\n"
    );
    // Retiring cut the cached compiled form, so the run sees it at once.
    let refused = call(
        &host.kernel,
        &Capability::root(),
        Verb::Source,
        "urn:script:old:result",
        &[],
    );
    assert!(matches!(refused, Err(Error::Conflict(_))), "{refused:?}");
    let refused = call(
        &host.kernel,
        &Capability::root(),
        Verb::Sink,
        "urn:script:old:runs",
        &[],
    );
    assert!(matches!(refused, Err(Error::Conflict(_))), "{refused:?}");
    // Still readable, and its version still fetchable.
    assert_eq!(ok(&host.kernel, Verb::Source, "urn:script:old", &[]), "1");
    assert_eq!(ok(&host.kernel, Verb::Source, &version, &[]), "1");
    // Retiring twice is not an error.
    assert!(ok(&host.kernel, Verb::Delete, "urn:script:old", &[]).contains("already"));
    // Publishing again brings it back.
    publish(&host.kernel, "old", "1", &[]);
    assert_eq!(
        ok(&host.kernel, Verb::Source, "urn:script:old:result", &[]),
        "1"
    );
}

#[test]
fn a_draft_is_saved_but_does_not_run() {
    let host = host();
    publish(&host.kernel, "wip", "(+ 1 1)", &[("state", "draft")]);
    let refused = call(
        &host.kernel,
        &Capability::root(),
        Verb::Source,
        "urn:script:wip:result",
        &[],
    );
    assert!(matches!(refused, Err(Error::Conflict(_))), "{refused:?}");
    assert_eq!(host.evals(), 0);
}

#[test]
fn the_catalog_lists_state_version_and_last_run() {
    let host = host();
    publish(&host.kernel, "a", "1", &[]);
    publish(&host.kernel, "b", "2", &[("public", "true")]);
    ok(&host.kernel, Verb::Sink, "urn:script:a:runs", &[]);
    let catalog: Catalog = serde_json::from_str(&ok(
        &host.kernel,
        Verb::Source,
        "urn:script:catalog",
        &[("as", "application/json")],
    ))
    .unwrap();
    let names: Vec<&str> = catalog.scripts.iter().map(|e| e.name.as_str()).collect();
    assert_eq!(names, vec!["a", "b"]);
    let a = &catalog.scripts[0];
    assert_eq!(a.last_run.as_ref().unwrap().iri, "urn:script:a:run:1");
    assert_eq!(a.last_run.as_ref().unwrap().status, "ok");
    assert_eq!(catalog.scripts[1].public, Some(true));
    let text = ok(&host.kernel, Verb::Source, "urn:script:catalog", &[]);
    assert!(text.contains("urn:script:b  published public"), "{text}");
}

#[test]
fn names_are_checked() {
    let host = host();
    for bad in ["eval-me:x", "Upper", "public", "outcome", "principal"] {
        let iri = format!("urn:script:{bad}");
        let result = call(
            &host.kernel,
            &Capability::root(),
            Verb::Sink,
            &iri,
            &[("content", "1")],
        );
        assert!(
            matches!(result, Err(Error::InvalidArgument { .. })),
            "{bad}: {result:?}"
        );
    }
}

#[test]
fn requires_refuses_what_attenuation_could_not_keep() {
    let host = host();
    for bad in ["urn:cap:net:*", "urn:cap:fs:read:-/secret", "lisp"] {
        let result = call(
            &host.kernel,
            &Capability::root(),
            Verb::Sink,
            "urn:script:x",
            &[("content", "1"), ("requires", bad)],
        );
        assert!(
            matches!(result, Err(Error::InvalidArgument { .. })),
            "{bad}: {result:?}"
        );
    }
}

/// Ledger #1076: a version's answer is a projection of its SCRIPT's state (whether that
/// content has been written under the name, and who may read the name), so it must hang
/// from the thread a publish or a retire cuts, which is the script's, not only its own.
#[test]
fn a_version_asked_for_before_it_was_published_is_found_after() {
    let host = host();
    // Learn a digest without writing it under `later`: a version is named by its content,
    // never by the script it belongs to.
    let elsewhere = publish(&host.kernel, "elsewhere", "(+ 40 2)", &[]);
    let digest = elsewhere.rsplit(":version:").next().unwrap().to_string();
    let version = format!("urn:script:later:version:{digest}");
    assert_eq!(ok(&host.kernel, Verb::Exists, &version, &[]), "false\n");
    // Asked twice: the negative is still worth caching (it is a backend read), as long as
    // the write that changes it cuts it.
    assert_eq!(ok(&host.kernel, Verb::Exists, &version, &[]), "false\n");
    assert_eq!(publish(&host.kernel, "later", "(+ 40 2)", &[]), version);
    assert_eq!(
        ok(&host.kernel, Verb::Exists, &version, &[]),
        "true\n",
        "a cached `false` outlived the publish that made the version"
    );
    assert_eq!(ok(&host.kernel, Verb::Source, &version, &[]), "(+ 40 2)");
}

#[test]
fn a_public_version_is_not_served_from_the_cache_once_its_script_is_retired() {
    let host = host();
    let version = publish(&host.kernel, "shown", "1", &[("public", "true")]);
    let reader = cap(&[ikigai_script::authority::CAP_READ_PUBLIC]);
    let read = |verb| call(&host.kernel, &reader, verb, &version, &[]);
    assert_eq!(read(Verb::Exists).unwrap(), "true\n");
    assert_eq!(read(Verb::Source).unwrap(), "1");
    ok(&host.kernel, Verb::Delete, "urn:script:shown", &[]);
    // Retired is no longer public, so the public grant reaches nothing: the same answer an
    // uncached read gives, and the one the head itself gives.
    assert!(
        matches!(read(Verb::Exists), Err(Error::Denied(_))),
        "{:?}",
        read(Verb::Exists)
    );
    assert!(
        matches!(read(Verb::Source), Err(Error::Denied(_))),
        "{:?}",
        read(Verb::Source)
    );
}