package main // Exhibit 20. An app's errors (rc.log("error", ...), and a trap's one line with its wasm backtrace) // and its jobs' output go to the journal; every minute root copies the last 200 lines into the fact // log.txt, which the app can read, and its creator reads on the app's page at apps (Health, the log's // last 40 lines), without a shell. Here, only the zoo's deliberate errors are shown, since the zoo is // public. // // The check does both halves: the zoo makes an error and finds the previous one in its own log.txt; // and, as a creator would, the robot makes one in the sample app zoo-scratch (`POST /error?mark=`) and // finds the previous one on zoo-scratch's page at apps. apps shows an app's page to its creator and the // owners (on staging, the samples to anyone signed in), so on live the robot reads it once zoo-scratch // is the robot's (`rc creator transfer zoo-scratch johannes zoo-robot`, the owner's); until then the // live check says so and proves only that apps shows the robot its own app's page. import "core:fmt" import "core:strings" import "ratcloud:rc" ERROR_MARK :: "zoo exhibit 20: a deliberate warning" ERROR_APP :: "zoo-scratch" error_lines :: proc() -> []string { text, _ := rc.fact("log.txt") out := make([dynamic]string) for l in strings.split_lines(text) do if strings.contains(l, ERROR_MARK) do append(&out, l) return out[max(0, len(out) - 6):] } Apps_Said :: struct { ok: bool `json:"ok"`, said: string `json:"said"`, line: string `json:"line,omitempty"`, } // The creator's half, as the robot: make an error in ERROR_APP and look on its page at apps for the // one the previous run made. error_apps :: proc() -> Apps_Said { b, err := robot_at("apps") if err != "" do return {false, err, ""} base := rc.app_url("apps") a := get(b, fmt.tprintf("%s/api/app/%s", base, ERROR_APP)) if a.status != 200 || a.j == nil { // Not the robot's on live yet: apps must still show the robot its own app's page. c2 := get(b, cat(base, "/api/app/zoo-broken")).status if env().env == "live" && a.status == 404 && c2 == 200 do return {true, "zoo-scratch's page at apps is its creator's, not the robot's, so the robot reads its log there once zoo-scratch is the robot's; apps shows the robot its own zoo-broken's page", ""} return {false, fmt.tprintf("apps did not show %s's page to the robot (%d)", ERROR_APP, a.status), ""} } prev, _ := meta("error_mark_apps") mark := random_hex(4) hs := make(map[string]string) hs["X-RC"] = "1" if made := go(b, "POST", fmt.tprintf("%s/error?mark=%s", rc.app_url(ERROR_APP), mark), extra = hs); made.status != 200 do return {false, fmt.tprintf("%s's /error answered %d", ERROR_APP, made.status), ""} meta_set("error_mark_apps", fmt.tprintf("%s@%d", mark, now())) if prev == "" do return {true, fmt.tprintf("the robot made its first error in %s; the next check looks for it on the app's page at apps", ERROR_APP), ""} p := strings.split(prev, "@") at := atoi(p[1]) if len(p) > 1 else 0 mins := (now() - at) / 60 for l in jarr(a.j, "log") { line := jstr(l) if strings.contains(line, p[0]) do return {true, fmt.tprintf("the robot made an error in %s and found the one it made %d minutes ago on the app's page at apps", ERROR_APP, mins), line} } // apps's view is root's, rewritten every minute: a younger error may not be there yet. if now() - at < 130 do return {true, fmt.tprintf("the robot made an error in %s; the previous one is too recent to look for on apps", ERROR_APP), ""} return {false, fmt.tprintf("the error the robot made in %s %d minutes ago is not on the app's page at apps", ERROR_APP, mins), ""} } E20_Data :: struct { apps: Apps_Said `json:"apps"`, } e20 :: proc() -> Exhibit { return { n = 20, wing = "Building", title = "Errors you can read", promise = "An app's error log is visible to its creator without a shell.", block = "rc.log(\"error\", ...), a trap's backtrace and job output land in the fact log.txt within a minute, readable by the app, and on the app's page at apps for its creator (Health: the log).", src = {"e20_errors.odin", #load("e20_errors.odin", string)}, script = {"web/e20.js", #load("web/e20.js", string)}, show = proc(me: ^rc.User) -> string { l := error_lines() d, _, _ := stored_data(20, E20_Data) b := strings.builder_make() rc.render_to(&b, `

It appears below within a minute, and on the zoo's page at apps for its creator.

{{2}}
`, {{"1", rc.app_url("apps")}, {"2", strings.join(l, "\n") if len(l) > 0 else "no deliberate errors in the log yet"}}) if d.apps.said != "" { rc.render_to(&b, `

The check also reads a log where a creator does, on the sample {{1}}: {{2}}`, {{"1", ERROR_APP}, {"2", d.apps.said}}) if d.apps.line != "" do rc.render_to(&b, `:

{{1}}
`, {{"1", d.apps.line}}) else do strings.write_string(&b, ".

") } return strings.to_string(b) }, api = proc(a: ^Api) -> bool { if a.do_ != "error" || !a.post do return false mark := random_hex(4) rc.log("error", fmt.tprintf("%s, %s, %s", ERROR_MARK, env().env, mark)) reply_raw(a, fmt.tprintf(`{{"made":%s}}`, jq(mark))) return true }, // Each run makes an error, and looks for the one the previous run made (root copies the log // every minute, and the check runs every five): in the zoo's own log.txt, and as the robot, in // zoo-scratch's log on its page at apps. check = proc() -> Check { prev, _ := meta("error_mark") r := post(new_browser(), self_url("/api/20/error")) made := jstr(r.j, "made") if made == "" do return fail(`the "make an error" button did not answer`) meta_set("error_mark", fmt.tprintf("%s@%d", made, now())) apps := error_apps() if prev == "" do return fail("made the first error; the next check looks for it in the log", E20_Data{apps}) p := strings.split(prev, "@") at := atoi(p[1]) if len(p) > 1 else 0 found := false for l in error_lines() do if strings.contains(l, p[0]) do found = true mins := (now() - at) / 60 if !found && now() - at >= 70 do return fail(fmt.tprintf("the error made %d minutes ago is not in the fact log.txt", mins), E20_Data{apps}) if !apps.ok do return fail(cat("apps: ", apps.said), E20_Data{apps}) first := fmt.tprintf("the error made %d minutes ago is in the fact log.txt", mins) if found else "made an error; the previous one is too recent to look for" return pass(cat(first, "; apps: ", apps.said), E20_Data{apps}) }, } }