fauna-api 1.0.858

The Fauna SDK for Rust: accounts, Drive, groups, calendar, chats, payments, Parrot and live rooms.
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
//! Termite's look, kept for people to review: a machine agent arranges the desktop and takes its
//! pictures and a recording, and Blender renders a procedural tree on a machine of its own.

use std::time::{Duration, Instant};

use futures_util::future::BoxFuture;

use super::agent::agent;
use super::media::keep;
use super::seat::{self, tool};
use super::{Benchmark, Run, Work, bytes, ensure, fail, mib, number, short, since};
use crate::error::Result;
use crate::private::benchmarks::BenchmarkGroup;
use crate::termite::os::{ExecOptions, MachineTool};
use crate::termite::spec::{STANDARD_SPEC, Spec};

const GALLERY_PORT: u16 = 8125;
const QT_APP: &str = "qalculate-qt";
/// The page the recorded task signs up on: an email field across the top and a button filling
/// most of the lower part.
const SIGNUP_PAGE: &str = r#"<!doctype html><title>Sign up</title><style>
*{box-sizing:border-box}body{margin:0;background:#f4f1ee;color:#1d1d1f;font:18px/1.4 'IBM Plex Sans',sans-serif}
form{display:grid;grid-template-rows:40% 60%;justify-items:center;align-items:center;height:100vh}
label{display:grid;gap:8px;width:60%;font-weight:600}input{padding:14px 16px;border:2px solid #d6d0ca;border-radius:10px;font:inherit;font-weight:400}
input:focus{outline:none;border-color:#a31b28}button{width:60%;height:70%;border:0;border-radius:18px;background:#a31b28;color:#fff;font:600 28px 'IBM Plex Sans',sans-serif}
</style><form onsubmit="event.preventDefault();this.querySelector('button').textContent='Welcome, '+this.email.value.split('@')[0]"><label>Email<input name="email" type="email" placeholder="you@example.com"></label><button>Sign up</button></form>"#;
const NOTES: &str = "# Termite\n\nThe desktop's own windows: a terminal, this editor, files, a preview and a Web window.\n";
/// entry.sh runs on every boot, so the page is served before the agent asks for it.
const SERVE_GALLERY: &str = "#!/usr/bin/env bash\ncp /opt/fauna/desktop/crown.png /repository/gallery/crown.png\ncd /repository/gallery && exec python3 -m http.server 8125\n";
/// Photos the gallery asks for, by title, and the recording.
const PHOTOS: [&str; 5] = ["Desktop", "Launcher", "GTK app", "Qt app", "Signed up"];

pub(super) struct Gallery;

/// What the gallery's agent is asked, step by step.
fn goal() -> String {
    format!(
        "This is Termite's gallery, for people to review how the desktop looks. Do each step in order, and take each photo with exactly the title given, with no outline unless asked.
1. Open a terminal window that runs `ls -la gallery`, an editor on gallery/notes.md, a files window on gallery, a preview of gallery/crown.png, and a Web window on http://localhost:{GALLERY_PORT}/.
2. Arrange those five windows in a tidy grid that fills the desktop above the dock: the terminal, editor and files windows across the top, the preview and the Web window (two columns wide) across the bottom, with even gaps. Then take a photo titled \"Desktop\".
3. Press the Super key to open the launcher over the grid, take a photo titled \"Launcher\", press Escape, and close all five windows.
4. Start galculator (`galculator &` from exec), put its window in the middle of the desktop, take a photo titled \"GTK app\", and close it.
5. Install {QT_APP} with `sudo fauna-install {QT_APP}`, start it, put its window in the middle of the desktop, take a photo titled \"Qt app\", and close it.
6. Open a Web window on http://localhost:{GALLERY_PORT}/ about 1200 by 700 near the top left, start a recording titled \"Signing up\", caption what happens as you click the email field, type ada@example.com, and press Sign up, then stop the recording once the page says welcome.
7. Take a photo titled \"Signed up\" with the button you clicked outlined. Then you're done."
    )
}

impl Benchmark for Gallery {
    fn id(&self) -> &'static str {
        "gallery"
    }
    fn name(&self) -> &'static str {
        "Gallery"
    }
    fn group(&self) -> BenchmarkGroup {
        BenchmarkGroup::Machines
    }
    fn about(&self) -> &'static str {
        "Termite's look for review: a machine agent arranges every built in window kind, opens the launcher, shows a GTK and a Qt app, and records a short task with its photo."
    }
    fn work(&self) -> Work {
        Work {
            steps: 80.0,
            machine_hours: 0.5,
            ..Work::default()
        }
    }
    fn timeout(&self) -> Duration {
        Duration::from_secs(30 * 60)
    }
    fn run<'a>(&'a self, run: &'a Run) -> BoxFuture<'a, Result<String>> {
        Box::pin(async move {
            run.progress(0.0, "Writing the gallery's files");
            let repository = run.repository("gallery").await?;
            repository
                .write("/repository/gallery/index.html", SIGNUP_PAGE, None)
                .await?;
            repository
                .write("/repository/gallery/notes.md", NOTES, None)
                .await?;
            repository
                .write("/repository/entry.sh", SERVE_GALLERY, None)
                .await?;
            run.progress(
                0.1,
                "The agent arranging the desktop and taking its pictures",
            );
            let done = agent(&run.key, repository.id, &goal(), 80).await?;
            run.progress(0.9, "Keeping the pictures");
            let kept = keep(run, repository.id, "").await?;
            let photos = kept.iter().filter(|kept| !kept.captioned_video()).count();
            let recorded = kept.iter().any(|kept| kept.captioned_video());
            run.measure(number("Photos", photos as f64, ""));
            run.measure(number(
                "Agent steps",
                done["steps"].as_f64().unwrap_or(0.0),
                "",
            ));
            ensure(done["ok"].as_bool() == Some(true), || {
                format!(
                    "The agent didn't finish: {}",
                    short(done["reason"].as_str().unwrap_or("it gave no reason"))
                )
            })?;
            ensure(photos >= PHOTOS.len() && recorded, || {
                format!(
                    "The agent kept {photos} of the {} photos and {} captioned recording.",
                    PHOTOS.len(),
                    if recorded { "a" } else { "no" }
                )
            })?;
            Ok(format!(
                "{photos} photos and a captioned recording of the desktop, the launcher, a GTK app, a Qt app and a signup."
            ))
        })
    }
}

// ─── Blender ─────────────────────────────────────────────────────────────────

pub(super) struct Blender;

/// The small pool's largest machine, so Blender renders on one small node.
const BLENDER: Spec = Spec {
    vcpus: 7,
    memory_bytes: 16 << 30,
    storage_bytes: 32 << 30,
    ..STANDARD_SPEC
};
/// How long installing Blender may take, and then its render.
const BLENDER_MS: u64 = 20 * 60_000;
/// The theme's bark (blood), leaves (success), ground (ink) and sky (soot), as sRGB.
const COLORS: [&str; 4] = ["110,16,24", "143,191,127", "11,11,12", "22,22,23"];
/// Blender's script for the gallery's tree, run in the background with the bark, leaves, ground
/// and sky colors after `--`.
const TREE_SCRIPT: &str = r#""""The Termite gallery's tree: a trunk and branches splitting recursively as cylinders, leaves
as small icospheres, on a ground plane under a sun, with a camera orbiting it. Writes tree.obj
(the tree, Y up) and tree.mp4 (a 4 second turntable at 640x360, Cycles on the CPU) beside
itself. Run as blender -b --factory-startup -P tree.py -- BARK LEAVES GROUND SKY, each an sRGB
r,g,b."""

import math
import os
import random
import sys

import bmesh
import bpy
from mathutils import Matrix, Vector

HERE = os.path.dirname(os.path.abspath(__file__))
FRAMES = 4 * 24
BARK, LEAVES, GROUND, SKY = (
    [int(channel) for channel in color.split(",")] for color in sys.argv[sys.argv.index("--") + 1 :]
)

random.seed(7)
for made in list(bpy.data.objects):
    bpy.data.objects.remove(made)
scene = bpy.context.scene


def linear(rgb):
    def channel(value):
        value /= 255
        return value / 12.92 if value <= 0.04045 else ((value + 0.055) / 1.055) ** 2.4

    return (*map(channel, rgb), 1.0)


def solid(name, built, rgb, roughness):
    mesh = bpy.data.meshes.new(name)
    built.to_mesh(mesh)
    built.free()
    mesh.polygons.foreach_set("use_smooth", [True] * len(mesh.polygons))
    material = bpy.data.materials.new(name)
    material.use_nodes = True
    shader = material.node_tree.nodes["Principled BSDF"]
    shader.inputs["Base Color"].default_value = linear(rgb)
    shader.inputs["Roughness"].default_value = roughness
    mesh.materials.append(material)
    made = bpy.data.objects.new(name, mesh)
    scene.collection.objects.link(made)
    return made


def turned(direction, spread, around):
    tilted = Matrix.Rotation(spread, 3, direction.orthogonal().normalized()) @ direction
    return (Matrix.Rotation(around, 3, direction) @ tilted + Vector((0, 0, 0.3))).normalized()


def grow(bark, leaves, start, direction, length, radius, depth):
    end = start + direction * length
    along = direction.to_track_quat("Z", "Y").to_matrix().to_4x4()
    bmesh.ops.create_cone(
        bark,
        cap_ends=True,
        segments=12,
        radius1=radius,
        radius2=radius * 0.7,
        depth=length,
        matrix=Matrix.Translation((start + end) / 2) @ along,
    )
    if depth == 0:
        for _ in range(8):
            spot = end + Vector([random.uniform(-0.35, 0.35) for _ in range(3)])
            size = random.uniform(0.12, 0.2)
            bmesh.ops.create_icosphere(leaves, subdivisions=1, radius=size, matrix=Matrix.Translation(spot))
        return
    for n in range(3):
        around = n * 2 * math.pi / 3 + random.uniform(-0.4, 0.4)
        child = turned(direction, random.uniform(0.45, 0.75), around)
        grow(bark, leaves, end, child, length * random.uniform(0.62, 0.78), radius * 0.62, depth - 1)


def write_obj(path, things):
    with open(path, "w") as out:
        first = 1
        for made in things:
            out.write(f"o {made.name}\n")
            for vertex in made.data.vertices:
                x, y, z = vertex.co
                out.write(f"v {x:.4f} {z:.4f} {-y:.4f}\n")
            for face in made.data.polygons:
                out.write("f " + " ".join(str(first + index) for index in face.vertices) + "\n")
            first += len(made.data.vertices)


bark = bmesh.new()
leaves = bmesh.new()
grow(bark, leaves, Vector((0, 0, 0)), Vector((0, 0, 1)), 2.2, 0.2, 4)
ground = bmesh.new()
bmesh.ops.create_grid(ground, x_segments=1, y_segments=1, size=12)
tree = [solid("bark", bark, BARK, 0.8), solid("leaves", leaves, LEAVES, 0.5)]
solid("ground", ground, GROUND, 0.9)
write_obj(os.path.join(HERE, "tree.obj"), tree)

sun = bpy.data.objects.new("sun", bpy.data.lights.new("sun", "SUN"))
sun.data.energy = 4
sun.data.angle = math.radians(4)
sun.rotation_euler = (math.radians(50), 0, math.radians(35))
scene.collection.objects.link(sun)
scene.world = bpy.data.worlds.new("sky")
scene.world.use_nodes = True
scene.world.node_tree.nodes["Background"].inputs["Color"].default_value = linear(SKY)

focus = bpy.data.objects.new("focus", None)
focus.location = (0, 0, 3)
pivot = bpy.data.objects.new("pivot", None)
camera = bpy.data.objects.new("camera", bpy.data.cameras.new("camera"))
camera.data.lens = 30
camera.location = (11, 0, 3.6)
camera.parent = pivot
aim = camera.constraints.new("TRACK_TO")
aim.target = focus
aim.track_axis = "TRACK_NEGATIVE_Z"
aim.up_axis = "UP_Y"
for made in (focus, pivot, camera):
    scene.collection.objects.link(made)
scene.camera = camera
bpy.context.preferences.edit.keyframe_new_interpolation_type = "LINEAR"
for frame, turn in ((1, 0), (FRAMES + 1, 2 * math.pi)):
    pivot.rotation_euler.z = turn
    pivot.keyframe_insert("rotation_euler", index=2, frame=frame)

scene.render.engine = "CYCLES"
scene.cycles.device = "CPU"
scene.cycles.samples = 16
scene.cycles.use_denoising = False
scene.render.resolution_x = 640
scene.render.resolution_y = 360
scene.render.resolution_percentage = 100
scene.render.fps = 24
scene.frame_start = 1
scene.frame_end = FRAMES
scene.render.use_persistent_data = True
scene.render.image_settings.file_format = "FFMPEG"
scene.render.ffmpeg.format = "MPEG4"
scene.render.ffmpeg.codec = "H264"
scene.render.ffmpeg.constant_rate_factor = "MEDIUM"
scene.render.filepath = os.path.join(HERE, "tree.mp4")
bpy.ops.render.render(animation=True)
"#;

/// An OBJ's vertices and faces.
pub(super) fn mesh(obj: &str) -> (usize, usize) {
    let count = |kind: &str| obj.lines().filter(|line| line.starts_with(kind)).count();
    (count("v "), count("f "))
}

impl Benchmark for Blender {
    fn id(&self) -> &'static str {
        "blender"
    }
    fn name(&self) -> &'static str {
        "Blender"
    }
    fn group(&self) -> BenchmarkGroup {
        BenchmarkGroup::Machines
    }
    fn about(&self) -> &'static str {
        "Blender installed from Debian on the small pool's largest machine, building a procedural tree and rendering a 4 second turntable with Cycles on the CPU, timed."
    }
    fn work(&self) -> Work {
        Work {
            machine_hours: 1.0,
            ..Work::default()
        }
    }
    fn timeout(&self) -> Duration {
        Duration::from_secs(60 * 60)
    }
    fn run<'a>(&'a self, run: &'a Run) -> BoxFuture<'a, Result<String>> {
        Box::pin(async move {
            run.progress(0.0, "Starting the machine");
            let repository = run.repository("blender").await?;
            let (room, _) = seat::boot(&run.key, repository.id, BLENDER).await?;
            let long = || ExecOptions {
                timeout_ms: Some(BLENDER_MS),
            };
            let rendered = async {
                run.progress(0.1, "Installing Blender");
                let from = Instant::now();
                let installed = room
                    .exec(
                        "sudo fauna-install blender >/tmp/blender-install.log 2>&1 || { tail -n 20 /tmp/blender-install.log; exit 1; }",
                        long(),
                    )
                    .await?;
                if installed.code != 0 {
                    return fail(format!(
                        "Installing Blender failed: {}",
                        installed.stdout.trim()
                    ));
                }
                run.measure(number("Install", since(from, Instant::now()) / 1000.0, "s"));
                tool(
                    &room,
                    MachineTool::Write {
                        path: "blender/tree.py".into(),
                        text: TREE_SCRIPT.into(),
                    },
                )
                .await?;
                run.progress(0.35, "Rendering the turntable");
                let command = format!(
                    "blender -b --factory-startup -noaudio --python-exit-code 1 -P blender/tree.py -- {} >/tmp/blender.log 2>&1 && test -s blender/tree.mp4 || {{ tail -n 30 /tmp/blender.log; exit 1; }}",
                    COLORS.join(" ")
                );
                let from = Instant::now();
                let rendered = room.exec(&command, long()).await?;
                let took = since(from, Instant::now());
                if rendered.code != 0 {
                    return fail(format!(
                        "Blender didn't render the tree: {}",
                        rendered.stdout.trim()
                    ));
                }
                run.measure(number("Render", took / 1000.0, "s"));
                Ok(took)
            }
            .await;
            let took = match rendered {
                Ok(took) => took,
                Err(err) => {
                    let _ = room.stop().await;
                    return Err(err);
                }
            };
            run.progress(0.9, "Keeping the tree and its turntable");
            let obj = bytes(&repository, "/repository/blender/tree.obj").await?;
            let video = bytes(&repository, "/repository/blender/tree.mp4").await?;
            let _ = room.stop().await;
            let (vertices, faces) = mesh(&String::from_utf8_lossy(&obj));
            run.measure(number("Vertices", vertices as f64, ""));
            run.measure(number("Faces", faces as f64, ""));
            run.measure(number("Turntable", mib(video.len() as u64), "MiB"));
            run.file("tree.obj", "model/obj", obj).await?;
            run.file("tree.mp4", "video/mp4", video).await?;
            ensure(vertices >= 8 && faces >= 6, || {
                format!("tree.obj isn't a mesh: {vertices} vertices and {faces} faces.")
            })?;
            Ok(format!(
                "Blender rendered a {vertices} vertex tree's turntable in {:.1} s on {} vCPUs.",
                took / 1000.0,
                BLENDER.vcpus
            ))
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn counts_an_objs_vertices_and_faces() {
        let cube = "# cube\nv 0 0 0\nv 1 0 0\nvn 0 0 1\nvt 0 0\nf 1 2 3\nf 1/1 2/2 3/3\n";
        assert_eq!(mesh(cube), (2, 2));
        assert_eq!(mesh(""), (0, 0));
    }

    #[test]
    fn asks_for_every_photo_it_checks() {
        let goal = goal();
        for title in PHOTOS {
            assert!(goal.contains(&format!("\"{title}\"")), "{title}");
        }
    }
}