20. Errors you can read
An app's error log is visible to its creator without a shell.
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).
This is the code serving the zoo right now: the bytes this very program was built from (each file is embedded when the zoo is compiled), at commit
55c71513c5 (staging). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.
e20_errors.odin sha256 28e409bfc443 · raw
1package main 2 3// Exhibit 20. An app's errors (rc.log("error", ...), and a trap's one line with its wasm backtrace) 4// and its jobs' output go to the journal; every minute root copies the last 200 lines into the fact 5// log.txt, which the app can read, and its creator reads on the app's page at apps (Health, the log's 6// last 40 lines), without a shell. Here, only the zoo's deliberate errors are shown, since the zoo is 7// public. 8// 9// The check does both halves: the zoo makes an error and finds the previous one in its own log.txt; 10// and, as a creator would, the robot makes one in the sample app zoo-scratch (`POST /error?mark=`) and 11// finds the previous one on zoo-scratch's page at apps. apps shows an app's page to its creator and the 12// owners (on staging, the samples to anyone signed in), so on live the robot reads it once zoo-scratch 13// is the robot's (`rc creator transfer zoo-scratch johannes zoo-robot`, the owner's); until then the 14// live check says so and proves only that apps shows the robot its own app's page. 15 16import "core:fmt" 17import "core:strings" 18import "ratcloud:rc" 19 20ERROR_MARK :: "zoo exhibit 20: a deliberate warning" 21ERROR_APP :: "zoo-scratch" 22 23error_lines :: proc() -> []string { 24 text, _ := rc.fact("log.txt") 25 out := make([dynamic]string) 26 for l in strings.split_lines(text) do if strings.contains(l, ERROR_MARK) do append(&out, l) 27 return out[max(0, len(out) - 6):] 28} 29 30Apps_Said :: struct { 31 ok: bool `json:"ok"`, 32 said: string `json:"said"`, 33 line: string `json:"line,omitempty"`, 34} 35 36// The creator's half, as the robot: make an error in ERROR_APP and look on its page at apps for the 37// one the previous run made. 38error_apps :: proc() -> Apps_Said { 39 b, err := robot_at("apps") 40 if err != "" do return {false, err, ""} 41 base := rc.app_url("apps") 42 a := get(b, fmt.tprintf("%s/api/app/%s", base, ERROR_APP)) 43 if a.status != 200 || a.j == nil { 44 // Not the robot's on live yet: apps must still show the robot its own app's page. 45 c2 := get(b, cat(base, "/api/app/zoo-broken")).status 46 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", ""} 47 return {false, fmt.tprintf("apps did not show %s's page to the robot (%d)", ERROR_APP, a.status), ""} 48 } 49 prev, _ := meta("error_mark_apps") 50 mark := random_hex(4) 51 hs := make(map[string]string) 52 hs["X-RC"] = "1" 53 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), ""} 54 meta_set("error_mark_apps", fmt.tprintf("%s@%d", mark, now())) 55 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), ""} 56 p := strings.split(prev, "@") 57 at := atoi(p[1]) if len(p) > 1 else 0 58 mins := (now() - at) / 60 59 for l in jarr(a.j, "log") { 60 line := jstr(l) 61 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} 62 } 63 // apps's view is root's, rewritten every minute: a younger error may not be there yet. 64 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), ""} 65 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), ""} 66} 67 68E20_Data :: struct { 69 apps: Apps_Said `json:"apps"`, 70} 71 72e20 :: proc() -> Exhibit { 73 return { 74 n = 20, wing = "Building", 75 title = "Errors you can read", 76 promise = "An app's error log is visible to its creator without a shell.", 77 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).", 78 src = {"e20_errors.odin", #load("e20_errors.odin", string)}, 79 script = {"web/e20.js", #load("web/e20.js", string)}, 80 show = proc(me: ^rc.User) -> string { 81 l := error_lines() 82 d, _, _ := stored_data(20, E20_Data) 83 b := strings.builder_make() 84 rc.render_to(&b, `<p><button id="err">Make an error</button> <span class="out muted" id="err-out">It appears below within a minute, and on the zoo's page at <a href="{{1}}/zoo">apps</a> for its creator.</span></p><pre class="log">{{2}}</pre>`, {{"1", rc.app_url("apps")}, {"2", strings.join(l, "\n") if len(l) > 0 else "no deliberate errors in the log yet"}}) 85 if d.apps.said != "" { 86 rc.render_to(&b, `<p class="muted">The check also reads a log where a creator does, on the sample <code>{{1}}</code>: {{2}}`, {{"1", ERROR_APP}, {"2", d.apps.said}}) 87 if d.apps.line != "" do rc.render_to(&b, `:</p><pre class="log">{{1}}</pre>`, {{"1", d.apps.line}}) 88 else do strings.write_string(&b, ".</p>") 89 } 90 return strings.to_string(b) 91 }, 92 api = proc(a: ^Api) -> bool { 93 if a.do_ != "error" || !a.post do return false 94 mark := random_hex(4) 95 rc.log("error", fmt.tprintf("%s, %s, %s", ERROR_MARK, env().env, mark)) 96 reply_raw(a, fmt.tprintf(`{{"made":%s}}`, jq(mark))) 97 return true 98 }, 99 // Each run makes an error, and looks for the one the previous run made (root copies the log 100 // every minute, and the check runs every five): in the zoo's own log.txt, and as the robot, in 101 // zoo-scratch's log on its page at apps. 102 check = proc() -> Check { 103 prev, _ := meta("error_mark") 104 r := post(new_browser(), self_url("/api/20/error")) 105 made := jstr(r.j, "made") 106 if made == "" do return fail(`the "make an error" button did not answer`) 107 meta_set("error_mark", fmt.tprintf("%s@%d", made, now())) 108 apps := error_apps() 109 if prev == "" do return fail("made the first error; the next check looks for it in the log", E20_Data{apps}) 110 p := strings.split(prev, "@") 111 at := atoi(p[1]) if len(p) > 1 else 0 112 found := false 113 for l in error_lines() do if strings.contains(l, p[0]) do found = true 114 mins := (now() - at) / 60 115 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}) 116 if !apps.ok do return fail(cat("apps: ", apps.said), E20_Data{apps}) 117 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" 118 return pass(cat(first, "; apps: ", apps.said), E20_Data{apps}) 119 }, 120 } 121}
web/e20.js sha256 33b69506390e · raw
1document.getElementById("err")?.addEventListener("click", async () => { 2 const j = await zoo.call("/api/20/error", {}); 3 document.getElementById("err-out").textContent = j.error || ("Made error " + j.made + ". Reload in a minute to see it below."); 4});