package main // Exhibit 25. A model changes an app on request and shows it on the preview address; staging and live // change only when the creator ships. // // The workbench: each creator's own OpenCode at code., every app of theirs a project, reached // through apps (apps./api/work wakes it and hands a two-minute link that becomes code's // cookie). Once a day the `workbench` job drives it as the zoo's robot, exactly as a browser would: // sign in at account and apps, wake the robot's workbench at zoo-scratch's session, and prompt it // through OpenCode's own API on code.: make /ping answer a fresh word on a new branch, push // the branch, `ratcloud preview` it, then push main once. The robot's workbench key pushes any branch // but main and staging (`rc work robot zoo-robot on`), so that last push is refused, and the card // shows it. From outside: the preview answers the word, staging and live do not, zoo-scratch's main // did not move. It needs zoo-scratch to be the robot's (`rc creator transfer zoo-scratch johannes // zoo-robot`) and the robot's workbench key kept to its branches: both root's. // // Staging has no workbench (apps-staging's /api/work is a 404): the staging zoo checks that refusal. // // A run takes many minutes; the job that runs it waits for the workbench in steps, each inside the // job's own time (30 minutes, its interval). import "core:encoding/json" import "core:fmt" import "core:strings" import "core:time" import "ratcloud:rc" E25_APP :: "zoo-scratch" // A run gets 18 minutes (several times a normal run); the job runs every 30 // minutes and is stopped at 30, so a run that hangs is stopped before the next one starts. E25_RUN_SECS :: 1080 // A daily run, so its verdict is at most a day and a bit old; older than this, the runs stopped. E25_FRESH :: 26 * 3600 // A red run is tried again after this (each try is a model run on the robot's ledger). E25_RETRY :: 3 * 3600 WORDS := [?]string{"otter", "heron", "badger", "lynx", "marten", "plover", "vole", "wren"} fresh_word :: proc() -> string {return fmt.tprintf("%s-%s", WORDS[random_int(0, 7)], random_hex(3))} e25_host :: proc(env_: string) -> string { h_ := domain_host(rc.app_url(E25_APP)) dot := strings.index_byte(h_, '.') label := strings.trim_suffix(h_[:dot], "-staging") return cat(label if env_ == "live" else cat(label, "-", env_), h_[dot:]) } Ping :: struct { url: string `json:"url"`, status: int `json:"status"`, body: string `json:"body"`, has_word: bool `json:"has_word"`, } e25_ping :: proc(env_: string) -> Ping { url := fmt.tprintf("https://%s/ping", e25_host(env_)) r := rc.fetch({url = url, timeout_ms = 15_000}) return {url = url, status = r.status, body = clip(strings.trim_space(string(r.body)), 120)} } // Whether the workbench path can run, and why not. Read as the robot, on apps, as any creator sees it. e25_ready :: proc() -> (bool, string) { if env().env != "live" do return false, "staging has no workbench" b, err := robot_at("apps") if err != "" do return false, err apps := rc.app_url("apps") if get(b, fmt.tprintf("%s/api/app/%s", apps, E25_APP)).status != 200 do return false, fmt.tprintf("%s is not the robot's yet (root: rc creator transfer %s johannes %s)", E25_APP, E25_APP, CREATOR) j := get(b, cat(apps, "/api/apps")).j wb: json.Value for k in jarr(j, "machines") do if jbool(k, "workbench") {wb = k; break} if wb == nil do return false, fmt.tprintf("the robot has no workbench key yet (root: rc work robot %s on)", CREATOR) if jstr(wb, "refs") != "branches" do return false, fmt.tprintf("the robot's workbench key may still push main (root: rc work robot %s on)", CREATOR) if !jbool(wb, "on") do return false, "the robot's workbench key was removed on apps" return true, "" } // zoo-scratch's main as apps shows it to its creator. e25_main :: proc(b: ^Browser) -> string { for br in jarr(get(b, fmt.tprintf("%s/api/app/%s", rc.app_url("apps"), E25_APP)).j, "branches") do if jstr(br, "name") == "main" do return jstr(br, "commit") return "" } Main_Push :: struct { refused: bool `json:"refused"`, said: string `json:"said"`, tried: bool `json:"tried"`, } E25_Run :: struct { path: string `json:"path"`, at: string `json:"at"`, request: string `json:"request,omitempty"`, word: string `json:"word,omitempty"`, branch: string `json:"branch,omitempty"`, main_before: string `json:"main_before,omitempty"`, main_after: string `json:"main_after,omitempty"`, session: string `json:"session,omitempty"`, messages: int `json:"messages"`, answer: string `json:"answer,omitempty"`, preview: Ping `json:"preview"`, staging: Ping `json:"staging"`, live: Ping `json:"live"`, main_push: Main_Push `json:"main_push"`, seconds: i64 `json:"seconds"`, ok: bool `json:"ok"`, detail: string `json:"detail"`, said: string `json:"said,omitempty"`, why: string `json:"why,omitempty"`, } // The run on the robot's workbench (the `workbench` job). busy: both of the worker's workbench slots // are taken (no attempt: the next half hour tries again). e25_work_round :: proc() -> (out: E25_Run, busy: string) { t0 := now() b, err := robot_at("apps") if err != "" do return {path = "workbench", at = iso(t0), detail = err}, "" apps := rc.app_url("apps") word := fresh_word() branch := cat("ping-", word) pv := e25_host("preview") out = {path = "workbench", at = iso(t0), word = word, branch = branch} out.request = fmt.tprintf("Make GET /ping answer with exactly the text \"%s\" (plain text, status 200), in `handle` in main.odin (an Odin app on wasm), keeping everything else as it is. Do it on a new branch named `%s` made from main: commit, and push that branch with `git push origin %s` (not main). Then run `ratcloud preview %s` and check with `curl -s https://%s/ping` that the preview answers it. Last, run `git push origin HEAD:main` once and tell me exactly what the server answered: it should refuse, and that refusal is what I want to see, so do not retry it or work around it. Do not ask me anything: nobody is watching.", word, branch, branch, branch, pv) failing :: proc(out: ^E25_Run, t0: i64, why: string) -> E25_Run { out.ok, out.detail, out.seconds = false, why, now() - t0 return out^ } out.main_before = e25_main(b) // Wake the robot's workbench at zoo-scratch's session: apps's /api/work, as the page polls it. link, state := "", "" for i := 0; i < 120; i += 1 { r := get(b, fmt.tprintf("%s/api/work?app=%s", apps, E25_APP)) state = fmt.tprintf("%s: %s", jstr(r.j, "state") or_else "?", jstr(r.j, "message")) if r.j != nil else fmt.tprintf("HTTP %d", r.status) if r.status == 200 && jstr(r.j, "link") != "" {link = jstr(r.j, "link"); break} st := jstr(r.j, "state") if r.status != 200 || st == "failed" || st == "refused" || st == "signin" do return failing(&out, t0, cat("apps would not wake the robot's workbench: ", state)), "" rc.sleep(2000) } if link == "" { if strings.has_prefix(state, "busy") do return {}, state return failing(&out, t0, cat("the robot's workbench did not wake in 4 minutes: ", state)), "" } origin := link[:8 + (strings.index_byte(link[8:], '/') if strings.index_byte(link[8:], '/') >= 0 else len(link) - 8)] lr := get(b, link) // the two-minute link becomes code's cookie, then a 303 to the session if lr.status != 303 && lr.status != 302 do return failing(&out, t0, fmt.tprintf("code's link answered %d, not a way in", lr.status)), "" sid := "" if i := strings.index(lr.location, "/session/ses_"); i >= 0 { rest := lr.location[i + 9:] n := 4 for n < len(rest) && is_alnum(rest[n]) do n += 1 sid = rest[:n] } accept := make(map[string]string) accept["Accept"] = "application/json" if sid == "" { for s in jarr(go(b, "GET", cat(origin, "/api/session?limit=200"), extra = accept).j, "data") do if strings.has_suffix(jstr(s, "location", "directory"), cat("/", E25_APP)) do sid = jstr(s, "id") } if sid == "" do return failing(&out, t0, cat("no ", E25_APP, " session in the robot's workbench")), "" out.session = sid // The prompt, through OpenCode's own API behind apps's gate; then wait for the turn to end. b.timeout_ms = 30_000 pr := go(b, "POST", fmt.tprintf("%s/api/session/%s/prompt", origin, sid), fmt.tprintf(`{{"text":%s}}`, jq(out.request)), "application/json", accept) b.timeout_ms = 15_000 if pr.status != 200 do return failing(&out, t0, fmt.tprintf("the workbench took no prompt (HTTP %d)", pr.status)), "" seen, idle := false, false for now() < t0 + E25_RUN_SECS { rc.sleep(3000) a := go(b, "GET", cat(origin, "/api/session/active"), extra = accept) busy_ := true if a.status == 200 && a.j != nil do busy_ = jhas(a.j, "data", sid) if busy_ do seen = true if !busy_ && (seen || now() - t0 > 30) {idle = true; break} } msgs := go(b, "GET", fmt.tprintf("%s/api/session/%s/message?limit=30", origin, sid), extra = accept) said := msgs.body out.messages = len(jarr(msgs.j, "data")) last := "" for m in jarr(msgs.j, "data") do for p in jarr(m, "content") do if jstr(p, "type") == "text" do last = strings.trim_space(jstr(p, "text")) out.answer = clip(last, 400) // From outside: the preview has the word, staging and live do not, main did not move. out.preview, out.staging, out.live = e25_ping("preview"), e25_ping("staging"), e25_ping("live") for p in ([]^Ping{&out.preview, &out.staging, &out.live}) do p.has_word = strings.contains(p.body, word) out.main_after = e25_main(b) REFUSAL :: "a robot's workbench key may not push refs/heads/main" refusal := "" if i := strings.index(said, REFUSAL); i >= 0 { end := i + len(REFUSAL) for end < len(said) && said[end] != '"' && said[end] != '\\' do end += 1 refusal = said[i:end] } out.main_push = {refusal != "" && out.main_after == out.main_before, refusal, strings.contains(said, "HEAD:main")} bad := make([dynamic]string) if !idle do append(&bad, fmt.tprintf("the workbench was still at work after %d minutes", E25_RUN_SECS / 60)) if !out.preview.has_word do append(&bad, cat("the preview's /ping does not answer ", word)) if out.staging.has_word do append(&bad, "staging answers the word") if out.live.has_word do append(&bad, "live answers the word") if out.main_after != out.main_before do append(&bad, fmt.tprintf("zoo-scratch's main MOVED (%s to %s)", short(out.main_before, 7), short(out.main_after, 7))) else if refusal == "" do append(&bad, "the workbench's push to main shows no refusal" if out.main_push.tried else "the workbench did not try to push main") out.seconds = now() - t0 out.ok = len(bad) == 0 out.detail = strings.join(bad[:], "; ") if len(bad) > 0 else fmt.tprintf(`the robot's workbench made /ping answer %s on %s and previewed it: the preview answers it, staging and live do not, and its key pushing main was refused ("%s"), %d s`, word, branch, refusal, out.seconds) return out, "" } // The `workbench` job, every 30 minutes: a run when one is due. What it did, in words. e25_job :: proc() -> (string, bool) { if env().env != "live" do return "", false hour, _, _ := time.clock(time.now()) v, has := meta("e25.work") w: E25_Run if has do json.unmarshal_string(v, &w) at := parse_time(w.at) due := !has || v == "" || (w.ok && ymd(at) < ymd(now()) && hour >= 5) || (!w.ok && now() - at >= E25_RETRY) if !due do return "", false if ready, _ := e25_ready(); !ready do return "", false r, busy := e25_work_round() if busy != "" do return fmt.tprintf("exhibit 25: the worker's workbench slots are both in use (%s); the next half hour tries again", busy), true meta_set("e25.work", to_json(r)) return fmt.tprintf("exhibit 25 %s (workbench): %s", "ok" if r.ok else "FAILED", r.detail), true } e25 :: proc() -> Exhibit { return { n = 25, wing = "Building", title = "Change an app by asking", promise = "A model in your workbench changes your app and shows it running on the preview address; staging and live change only when you ship.", block = "The workbench (apps, Open the workbench): your own OpenCode at code., every app of yours a project, the model on your ai ledger. Ask; it works on a branch, pushes it and previews it (ratcloud preview); you ship from apps when it is right. A robot's workbench key never pushes main or staging.", src = {"e25_change_by_asking.odin", #load("e25_change_by_asking.odin", string)}, files = srcs({"robot.odin", #load("robot.odin", string)}), show = proc(me: ^rc.User) -> string { p, _, found := stored_data(25, E25_Run) apps := rc.app_url("apps") live := apps if i := strings.index(apps, "-staging."); i >= 0 do live = cat(apps[:i], apps[i + 8:]) // the workbench is live's alone yours := fmt.tprintf(`

Yours: Open the workbench on apps (creators, on live).

`, h(live)) if !found || p.path == "" do return cat("

Not checked yet.

", yours) if p.path == "staging" do return fmt.tprintf(`

This is staging: it has no workbench (%s). The live zoo runs this exhibit in its robot's workbench.

`, h(p.said)) line :: proc(x: Ping, label: string) -> string { return fmt.tprintf(`
  • %s: /ping answered %d %s%s
  • `, label, h(x.url), x.status, h(clip(x.body, 40)), " (has the word)" if x.has_word else " (without it)") } lines := cat(line(p.preview, "preview"), line(p.staging, "staging"), line(p.live, "live")) if p.path == "workbench" { return cat(fmt.tprintf(`

    The zoo's robot asked its own workbench (OpenCode at code.%s, through apps), %s, in %s:

    %s
      `, h(env().domain), ago(parse_time(p.at)), E25_APP, h(p.request)), lines, `
    • the robot's workbench key pushing main: `, cat("refused: ", h(p.main_push.said), "") if p.main_push.refused else "not refused", "
    ", fmt.tprintf(`

    %d messages in the session, %d s, on the robot's ai ledger (app apps). Path: the robot's workbench.

    `, p.messages, p.seconds), yours) } return cat(fmt.tprintf(`

    Not running: %s.

    `, h(p.why if p.why != "" else p.detail)), yours) }, check = proc() -> Check { if env().env != "live" { r := get(new_browser(), cat(rc.app_url("apps"), "/api/work")) said := jstr(r.j, "error") if r.status == 404 do return pass(fmt.tprintf(`staging has no workbench (apps-staging: "%s"); the live zoo runs this exhibit in its robot's workbench`, said), E25_Run{path = "staging", said = said, at = iso(now())}) return fail(fmt.tprintf("staging's apps answered %d for a workbench (it has none)", r.status), E25_Run{path = "staging", at = iso(now())}) } ready, why := e25_ready() if v, has := meta("e25.work"); ready && has && v != "" { w: E25_Run json.unmarshal_string(v, &w) age := now() - parse_time(w.at) if age > E25_FRESH do return fail(fmt.tprintf("the robot's workbench last ran this %d h ago: the daily runs stopped", age / 3600), w) if !w.ok do return fail(cat("the robot's workbench: ", w.detail), w) for e in ([]string{"staging", "live"}) do if strings.contains(e25_ping(e).body, w.word) do return fail(cat(e, " answers the workbench's word: a change reached ", e, " without a ship"), w) return pass(cat("the robot's workbench: ", w.detail), w) } if ready do why = "ready; its first run comes within half an hour" return fail(cat("the robot's workbench: ", why), E25_Run{path = "waiting", why = why, at = iso(now())}) }, } }