cuttlefish-host 0.9.0

Wasmtime host that drives cuttlefish proc-blocks and enforces capabilities
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
//! The `on_fail` recovery ladder, end to end through the real host.
//!
//! Every assertion here is about *how many times* and *against which
//! backend* work happened, not merely about the final answer. A ladder that
//! never retried, or that rerouted to the same model, would produce an
//! identical final value in most of these cases — so counting is the only
//! thing that actually pins the behaviour down.

mod support;

use cuttlefish_abi::JobStatus;
use cuttlefish_core::graph::{AcceptCheck, Rung};
use cuttlefish_core::spec::ModelRef;
use cuttlefish_host::caps::Capabilities;
use cuttlefish_host::catalog::ArtifactKind;
use cuttlefish_host::dag::CheckedNode;
use cuttlefish_host::infer::{InferBackend, InferRequest, InferResult};
use cuttlefish_host::ledger::Ledger;
use cuttlefish_host::module_cache::ModuleCache;
use cuttlefish_host::runner::{run_job, Alternates, JobSpec};
use std::collections::HashMap;
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use wasmtime::Engine;

fn interpreter_wasm() -> Vec<u8> {
    static WASM: std::sync::OnceLock<Vec<u8>> = std::sync::OnceLock::new();
    WASM.get_or_init(|| {
        let status = support::clean_cargo(env!("CARGO"))
            .args([
                "build",
                "-p",
                "cf-block-rhai-interpreter",
                "--target",
                "wasm32-unknown-unknown",
            ])
            .status()
            .expect("cargo build failed to start");
        assert!(status.success(), "building the rhai interpreter failed");
        let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
        std::fs::read(
            root.join("target/wasm32-unknown-unknown/debug/cf_block_rhai_interpreter.wasm"),
        )
        .unwrap()
    })
    .clone()
}

/// A backend that counts calls and tags its reply with its own name, so a
/// test can tell *which* backend answered.
struct CountingBackend {
    name: String,
    calls: Arc<AtomicUsize>,
}

#[async_trait::async_trait]
impl InferBackend for CountingBackend {
    async fn infer(
        &self,
        _req: InferRequest<'_>,
        _on_token: &mut (dyn for<'t> FnMut(&'t str) -> bool + Send),
    ) -> anyhow::Result<InferResult> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        Ok(InferResult {
            text: self.name.clone(),
            tokens_in: 0,
            tokens_out: 0,
        })
    }
    fn model_name(&self) -> String {
        self.name.clone()
    }
}

fn counting(name: &str) -> (Arc<dyn InferBackend>, Arc<AtomicUsize>) {
    let calls = Arc::new(AtomicUsize::new(0));
    let backend: Arc<dyn InferBackend> = Arc::new(CountingBackend {
        name: name.to_string(),
        calls: calls.clone(),
    });
    (backend, calls)
}

/// A node whose script echoes the model's reply, with `accept`/`on_fail`.
fn node(accept: Vec<AcceptCheck>, on_fail: Vec<Rung>) -> CheckedNode {
    CheckedNode {
        name: "work".to_string(),
        kind: ArtifactKind::Script,
        resolved: None,
        module_bytes: interpreter_wasm(),
        signature: cuttlefish_abi::Signature {
            input: cuttlefish_abi::Ty::Json,
            output: cuttlefish_abi::Ty::Json,
        },
        input: None,
        repeat_until: None,
        max_iterations: None,
        script: Some(r#"#{ from: infer("do the work", 32) }"#.to_string()),
        over: None,
        item_output: None,
        accept,
        on_fail,
    }
}

/// A schema demanding `from == "big"`, so only the strong model passes.
fn only_big_passes(dir: &Path) -> AcceptCheck {
    let path = dir.join("v.json");
    std::fs::write(
        &path,
        r#"{"type":"object","properties":{"from":{"const":"big"}},"required":["from"]}"#,
    )
    .unwrap();
    AcceptCheck::Schema(path)
}

async fn run(
    node: CheckedNode,
    backend: Arc<dyn InferBackend>,
    alternates: Alternates,
    ledger: &Ledger,
    dir: &Path,
) -> cuttlefish_abi::Envelope {
    let (tx, _rx) = mpsc::channel(1024);
    let job = JobSpec {
        nodes: vec![node],
        exclusive_to: HashMap::new(),
        input: serde_json::Value::Null,
        caps: Capabilities::new(vec![dir.to_path_buf()]),
        alternates,
        embedder: None,
        warehouse: None,
    };
    run_job(
        Arc::new(Engine::default()),
        backend,
        job,
        tx,
        CancellationToken::new(),
        ledger,
        &ModuleCache::new(),
    )
    .await
}

fn ledger_in(dir: &Path) -> Ledger {
    Ledger::open(&dir.join("ledger.sqlite"), "fp").unwrap()
}

#[tokio::test]
async fn a_node_with_no_ladder_attempts_once_and_fails() {
    // The unchanged default. Everything below is measured against this.
    let dir = tempfile::tempdir().unwrap();
    let (backend, calls) = counting("small");
    let envelope = run(
        node(vec![only_big_passes(dir.path())], vec![]),
        backend,
        Alternates::new(),
        &ledger_in(dir.path()),
        dir.path(),
    )
    .await;

    assert_eq!(envelope.status, JobStatus::Failed, "{envelope:?}");
    assert_eq!(
        calls.load(Ordering::SeqCst),
        1,
        "no ladder means one attempt"
    );
}

#[tokio::test]
async fn retry_makes_further_attempts_against_the_same_backend() {
    // `retry 2` = up to two *further* attempts, so three calls in total when
    // every one of them is rejected.
    let dir = tempfile::tempdir().unwrap();
    let (backend, calls) = counting("small");
    let envelope = run(
        node(vec![only_big_passes(dir.path())], vec![Rung::Retry(2)]),
        backend,
        Alternates::new(),
        &ledger_in(dir.path()),
        dir.path(),
    )
    .await;

    assert_eq!(envelope.status, JobStatus::Failed);
    assert_eq!(calls.load(Ordering::SeqCst), 3);
}

#[tokio::test]
async fn reroute_engages_only_after_retries_are_spent_and_uses_the_other_backend() {
    // The load-bearing test for the alternates map: an implementation that
    // resolved every model to the same backend would pass every other test
    // in this file, so this asserts on *which* backend answered.
    let dir = tempfile::tempdir().unwrap();
    let (small, small_calls) = counting("small");
    let (big, big_calls) = counting("big");
    let big_model = ModelRef::new("stub", "big");
    let mut alternates = Alternates::new();
    alternates.insert(big_model.clone(), big);

    let envelope = run(
        node(
            vec![only_big_passes(dir.path())],
            vec![Rung::Retry(1), Rung::Reroute(big_model)],
        ),
        small,
        alternates,
        &ledger_in(dir.path()),
        dir.path(),
    )
    .await;

    assert_eq!(envelope.status, JobStatus::Completed, "{envelope:?}");
    assert_eq!(
        envelope.result.unwrap()["from"],
        "big",
        "the accepted value must come from the rerouted model"
    );
    assert_eq!(
        small_calls.load(Ordering::SeqCst),
        2,
        "first attempt plus one retry, both on the original backend"
    );
    assert_eq!(big_calls.load(Ordering::SeqCst), 1, "then one reroute");
}

#[tokio::test]
async fn escalate_is_terminal_and_records_a_reason() {
    let dir = tempfile::tempdir().unwrap();
    let ledger = ledger_in(dir.path());
    let (backend, calls) = counting("small");

    let envelope = run(
        node(
            vec![only_big_passes(dir.path())],
            vec![Rung::Retry(1), Rung::Escalate],
        ),
        backend,
        Alternates::new(),
        &ledger,
        dir.path(),
    )
    .await;

    assert_eq!(envelope.status, JobStatus::Failed, "{envelope:?}");
    assert_eq!(
        calls.load(Ordering::SeqCst),
        2,
        "escalate consumes no attempt"
    );

    let escalations = ledger.escalations().unwrap();
    assert_eq!(escalations.len(), 1, "the give-up must be recorded");
    assert_eq!(escalations[0].node, "work");
    assert!(
        escalations[0].reason.contains("from"),
        "the reason must carry the failing check, or it's unactionable: {}",
        escalations[0].reason
    );
}

/// A backend that answers "small" once and "big" forever after — one
/// rejected attempt followed by an accepted one.
struct FlipsAfterFirst {
    calls: Arc<AtomicUsize>,
}

#[async_trait::async_trait]
impl InferBackend for FlipsAfterFirst {
    async fn infer(
        &self,
        _req: InferRequest<'_>,
        _on_token: &mut (dyn for<'t> FnMut(&'t str) -> bool + Send),
    ) -> anyhow::Result<InferResult> {
        let n = self.calls.fetch_add(1, Ordering::SeqCst);
        Ok(InferResult {
            text: if n == 0 { "small" } else { "big" }.to_string(),
            tokens_in: 0,
            tokens_out: 0,
        })
    }
    fn model_name(&self) -> String {
        "flips".to_string()
    }
}

#[tokio::test]
async fn a_rejected_attempt_leaves_no_checkpoint_behind() {
    // The durability rule: only a *concluded* outcome gets a ledger row. A
    // rejected attempt that is about to be retried is not a conclusion, and
    // recording it would make a transient rejection permanent across a
    // resume — the node would come back with the bad value already cached.
    let dir = tempfile::tempdir().unwrap();
    let ledger = ledger_in(dir.path());
    let calls = Arc::new(AtomicUsize::new(0));
    let backend: Arc<dyn InferBackend> = Arc::new(FlipsAfterFirst {
        calls: calls.clone(),
    });

    let envelope = run(
        node(vec![only_big_passes(dir.path())], vec![Rung::Retry(1)]),
        backend,
        Alternates::new(),
        &ledger,
        dir.path(),
    )
    .await;

    assert_eq!(envelope.status, JobStatus::Completed, "{envelope:?}");
    assert_eq!(calls.load(Ordering::SeqCst), 2, "one rejection, one retry");

    // Exactly one row, holding the *accepted* value — not the rejected one,
    // and not both.
    let checkpoint = ledger.get_completed("work").unwrap();
    assert_eq!(
        checkpoint,
        Some(serde_json::json!({"from": "big"})),
        "the checkpoint must hold what was accepted"
    );
    assert!(
        ledger.escalations().unwrap().is_empty(),
        "a ladder that succeeded must escalate nothing"
    );
}

#[tokio::test]
async fn an_accepted_first_attempt_climbs_no_rungs() {
    let dir = tempfile::tempdir().unwrap();
    let (backend, calls) = counting("big");
    let envelope = run(
        node(
            vec![only_big_passes(dir.path())],
            vec![Rung::Retry(5), Rung::Escalate],
        ),
        backend,
        Alternates::new(),
        &ledger_in(dir.path()),
        dir.path(),
    )
    .await;

    assert_eq!(envelope.status, JobStatus::Completed, "{envelope:?}");
    assert_eq!(calls.load(Ordering::SeqCst), 1, "success must not retry");
}

#[tokio::test]
async fn an_escalation_carries_the_input_it_gave_up_on() {
    // Without the input, an escalation names an item index against a
    // manifest that may since have moved or been deleted -- not enough to
    // act on. This is what makes the queue drainable.
    let dir = tempfile::tempdir().unwrap();
    let ledger = ledger_in(dir.path());
    let (backend, _) = counting("small");

    let job_input = serde_json::json!({"doc": "q3.pdf", "chunk": 12});
    let (tx, _rx) = mpsc::channel(1024);
    let job = JobSpec {
        nodes: vec![node(
            vec![only_big_passes(dir.path())],
            vec![Rung::Escalate],
        )],
        exclusive_to: HashMap::new(),
        input: job_input.clone(),
        caps: Capabilities::new(vec![dir.path().to_path_buf()]),
        alternates: Alternates::new(),
        embedder: None,
        warehouse: None,
    };
    let envelope = run_job(
        Arc::new(Engine::default()),
        backend,
        job,
        tx,
        CancellationToken::new(),
        &ledger,
        &ModuleCache::new(),
    )
    .await;
    assert_eq!(envelope.status, JobStatus::Failed, "{envelope:?}");

    let escalations = ledger.escalations().unwrap();
    assert_eq!(escalations.len(), 1);
    assert_eq!(
        escalations[0].input.as_ref(),
        Some(&job_input),
        "the input must round-trip verbatim, not merely be non-null"
    );
    assert!(
        escalations[0].drained_at.is_none(),
        "a fresh escalation is outstanding, not drained"
    );
}

#[tokio::test]
async fn a_successful_run_records_no_input_to_drain() {
    // Only failures pay the storage. A succeeded item's input is still in
    // the manifest and nobody needs it handed back.
    let dir = tempfile::tempdir().unwrap();
    let ledger = ledger_in(dir.path());
    let (backend, _) = counting("big");
    let envelope = run(
        node(vec![only_big_passes(dir.path())], vec![Rung::Escalate]),
        backend,
        Alternates::new(),
        &ledger,
        dir.path(),
    )
    .await;

    assert_eq!(envelope.status, JobStatus::Completed, "{envelope:?}");
    assert!(ledger.all_escalations().unwrap().is_empty());
}