diff --git a/AGENTS.md b/AGENTS.md index 23eefcf5a3..14780dd70f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -25,19 +25,21 @@ When using Bend: - `src/gfx/bitmap.bend`: raw RGB frames (the browser helper's frame file) as an `Image` quadtree, composited into a frame at a place and size. - `src/core/svg.bend` (pure: SVG path data parsed, written, flattened; laws `svg_*`) and `src/core/icons.bend` (the icon set, Lucide paths, ISC; laws `icons_*`). The window rasterizes them once into `SMask` bitmaps in the glyph cache (`src/gfx/icon.bend`, `Lay.icon`/`Lay.ibutton`); the web writes them as inline `` (`src/web/icon.bend`). Test: `test/svg_test.bend`. - `tools/browser/`: `backplane-browser`, a Bun + playwright-core headless Chromium driven by NDJSON on stdin/stdout; raw frames go to a file. Pages are keyed by an owner (`"page"`: a bot's id for a bot's thread, else the thread's id, `BT.World.page`, so every thread drives its own; `""` is the shared page, which Backplane no longer uses), all in one persistent profile (`/browser-profile`); each page's JPEG frames go to `/shots/.jpg`. Bend side: `src/server/browser.bend` (`Browser.ask_on`, test `test/browser_test.bend`). Protocol in its README; `bun tools/browser/test/pages.ts` tests it. Built by `scripts/build-browser.sh`. -- `src/core/client.bend`: the client state and actions both UIs share. Each thread (a bot's too) has its own viewer: kind, path, diff and whether the panel is open live under `:` in the info map (`Viewer.put`, `Ui.reselect` swaps the panel when the selection moves), and the window keeps each thread's camera and scene (`N.Park` in main.bend's `Kept`). The Browser tab shows the thread's own page (`Lay.page`). Test: `test/view_test.bend`; end to end `bun test/tools/viewers_e2e.ts`. +- `src/core/client.bend`: the client state and actions both UIs share. Each thread (a bot's too) has its own viewer: kind, path, diff and whether the panel is open live under `:` in the info map (`Viewer.put`, `Ui.reselect` swaps the panel when the selection moves), and the window keeps each thread's camera and scene (`N.Park` in main.bend's `Kept`). The Browser tab shows the thread's own page (`Lay.page`). `Want.*` binds a machine-switch selection to its destination and generation, and adoption resolves it after installing state; `HubCache.*` keeps the home and last remote hub for immediate display and delta reconnects (`test/hub_switch_test.bend`). Test: `test/view_test.bend`; end to end `bun test/tools/viewers_e2e.ts`. - `src/core/menu.bend` (pure: where a menu's boxes go: stacked rows, a wrapping flow of chips, rows that fit a height, a clamped scroll; laws `menu_*`) and `src/core/badge.bend` (a project's colour and icon from its settings; laws `badge_*`). The model picker (`Lay.picker` in layout.bend) and the badge editor (`Look.*`) place everything with them. Test: `test/menu_test.bend`. `test/native/ui_snap.bend` renders the window from an event log and an info message to a PPM with no window (`build/ui_snap EVENTS INFO THREAD FLAGS OUT [W H]`). - Projects in the sidebar: the search icon opens a filter (`Find.*` in client.bend: form field `pq`, flag `psearch`, actions `proj-find` (on/off/flip), `proj-find-q`, `proj-go`; laws `find_*`); the badge editor's Remove takes two clicks (`Ui.remove_project`, `@rm`, `proj-keep` cancels) and sends `project.remove`; adding a removed project's folder again brings it back with its threads (`Hub.removed_id`). Phones: a search field over the list (every hub's at once: `Hubs.everywhere`) and a long press on a project that asks first (`removing` in screen.bend). Tests: `test/project_find_test.bend`, `test/hubs_test.bend`. -- The sidebar (client.bend's Quiet/Active/Menu sections, all four clients): one list whatever machine it runs on (the machine switch sits beside Settings, only with several; phones merge every hub and name a machine only when two projects share a name, `Hubs.projects`). The search field is always at the top of the list (`Find.q` holds whenever it has text). Projects with no live thread sink under the live ones, faded, and after `Quiet.days` fold under "Older" (`Side.groups`, flag `old-projects`, action `side-older`). The "sidebar.view" setting "active" swaps in one list of live threads, what waits on the person first (`Urg.rank`: approval, question, failure, finished, stopped, working, idle; pinned ahead within each), then the recently settled, faded and folded under "Settled N" (`Active.rows`, `Active.parts`/`Active.folded`; flag `active-settled`, action `side-settled`, a search shows them; law `active_fold_shut`). A setting (the view swap included) applies to the client's state at once, before the hub echoes it (`Ui.setting.here`; the sequence is left alone). A right-click (a long press on phones) opens a row's or project's menu (`Menu.*`, scratch `@menu`, actions `row-menu`/`proj-menu`/`menu-close`, `row-pin`/`row-archive`...; Delete and Remove ask first); nothing acts on a row by itself. Laws `urg_*`, `menu_*`, `find_always_on`; test `test/sidebar_test.bend`. +- The sidebar (client.bend's Quiet/Active/Menu sections, all four clients): one list whatever machine it runs on (the machine switch sits beside Settings, only with several; phones merge every hub, label projects from a hub other than the focused one, and disambiguate equal names, `Hubs.projects`). The search field is always at the top of the list (`Find.q` holds whenever it has text). Projects with no live thread sink under the live ones, faded, and after `Quiet.days` fold under "Older" (`Side.groups`, flag `old-projects`, action `side-older`). The "sidebar.view" setting "active" swaps in one list of live threads, what waits on the person first (`Urg.rank`: approval, question, failure, finished, stopped, working, idle; pinned ahead within each), then the recently settled, faded and folded under "Settled N" (`Active.rows`, `Active.parts`/`Active.folded`; flag `active-settled`, action `side-settled`, a search shows them; law `active_fold_shut`). A setting (the view swap included) applies to the client's state at once, before the hub echoes it (`Ui.setting.here`; the sequence is left alone). A right-click (a long press on phones) opens a row's or project's menu (`Menu.*`, scratch `@menu`, actions `row-menu`/`proj-menu`/`menu-close`, `row-pin`/`row-archive`...; Delete and Remove ask first); nothing acts on a row by itself. Laws `urg_*`, `menu_*`, `find_always_on`; test `test/sidebar_test.bend`. - `src/core/refs.bend`: references in a message (pure: `@bot`, `>thread`, `%project`, `#room` slugs; what the composer offers, ranked; what a sent message names; the agent's `` block; summaries from the text model, kept as settings `summary.`; laws `refs_*`, `sum_*`, `bot_mention_once`). The composer's popup shows them through the `$skill` rows (`U.Skills.of`, rows named with their sigil), cached per draft edit in the info map (`refs.for`, `refs`). The hub wakes mentioned bots and posts to mentioned rooms (`BH.Bots.hub`); turn ends ask for summaries (`H.Sum.*`, the server's `SumJob`). See `docs/bots.md`. Test: `test/refs_test.bend`. - `src/core/links.bend`: links in a message (pure: web addresses, bare or `[label](url)`, and paths to files, bare, in backticks or as a markdown target, with an optional `:12`/`#L12`; `Link.pieces`, `Link.is_path`; laws `link_*`). A link that is not a web address is a file's: a click asks the hub to open it on its machine (`file-open` → `file.open {path, how}`, the hub's `FileJob` runs `Link.file.cmd`: the system opener, or "reveal" through the desktop's FileManager1, Finder on a Mac), a right-click (a tap on phones) opens its menu (client.bend's `Menu.file`, `@menu` `f:`: Open, Show in folder, Copy path; `file-menu`, `file-reveal`, `file-copy`). A relative path is in the thread's folder (`Flink.abs`, `Link.file.abs`). Tests: `test/links_test.bend`, `test/sidebar_test.bend`. - `src/core/fold.bend`: runs of tool calls folded into one row (pure: groups, labels, per-entry roles; open state is `fold:` in the Ui info map, the "fold" action; laws `fold_*`). The window applies the roles each frame over the memo (`TL.entries.mr`, kind 14 lines), the web in `View.entries`. Test: `test/fold_test.bend`. - `src/core/edit.bend`: text editing for the window's fields (word and line motions, ranges, undo history); `nui.bend`'s `Ed` maps keys onto it. The composer moves by the lines it shows: Up/Down and a press or drag in it leave a request in the scratch (`@cq`, nui.bend's `Cq`) that main.bend places after each event with the font (`Comp.pend` in layout.bend, over `Edit.starts`). Test: `test/edit_test.bend`. Thread text selection is `TSel` in `layout.bend` and `Sel.*` in `nui.bend` (test `test/select_test.bend`). -- `src/core/tailnet.bend` (pure: owner trust from `tailscale whois`, the owner's machines from `tailscale status`) and `src/app/link.bend` (a WebSocket client shaped like a hub, so the native window can switch to another machine's hub). Each hub finds the owner's other hubs (GET /hello) and lists them in the sidebar; `BACKPLANE_PEERS` names hubs off the tailnet. +- `src/core/tailnet.bend` (pure: owner trust from `tailscale whois`, the owner's machines from `tailscale status`) and `src/app/link.bend` (a WebSocket client shaped like a hub, so the native window can switch to another machine's hub). Link effects (`src/app/effects/link.c` and `.js`) resolve hostnames and bound the handshake to five seconds; the window reconnects from the cached sequence and origin. `Accept.whois` bounds identity lookup too. Each hub finds the owner's other hubs (GET /hello) and lists them in the sidebar; `BACKPLANE_PEERS` names hubs off the tailnet. - `src/core/diff.bend` (pure: unified diff → files, hunks, numbered lines; stats) and `src/server/git.bend` (IO: repo info, porcelain status, worktrees, hidden-ref checkpoints, stacked commit/push/PR). See `docs/git.md`. Native test: `bend test/native/git_test.bend -o build/git_test && build/git_test`. - `src/core/hier.bend`: hierarchical schematics (pure: the sheets a file lists, paths resolved beside their parent, the tree as pre-order rows, where a row sits, its parent and children; a sheet that includes an ancestor is listed but never expanded; laws `hier_*`). main.bend's `Tree.load` reads every file the root reaches after each schematic read and sends the rows (`Sheets`); the window keeps them in the scratch (`@vw.tree`, `@vw.at`) and shows a trail and the sheet list (`Side.*` in layout.bend; actions `vw-sheet`, `vw-open`, `vw-up`). Test: `test/hier_test.bend`; headless: `test/native/sheet_frame.bend ROOT.kicad_sch`. - The board viewer keeps its scene in (layer, key) order, which is paint order, so a frame is one pass (`V.Ops.scene`); items off the panel are culled before any geometry is transformed. The settled scene is drawn once per view into the frame's base by `src/gfx/fresh.bend` (`Fresh.frame`: the ops split into 64 px blocks, dealt over the cores, the image put together; the same image `R.Frame.render` draws), so a pan or zoom redraws it in parallel; the tiles compare a base only where their ops are the same (`Eq.tile`), and `Eq.image` stops at the first difference. Filled polygons are anti-aliased by distance to their nearest edge (`Cov.poly` in raster.bend). Bench: `test/native/viewer_bench.bend FILE [W H]` (pan, zoom and keys with the viewer open, each frame checked exact). Layers turn on and off per kind with a flip mask (`@vw.flip.`, `V.Look.on`; laws `look_flip_*`, `ops_hidden_draws_nothing`, `pick_hidden_skipped`). Bench: `test/native/board_frame.bend BOARD.kicad_pcb`. - Bots in the window: `src/app/bots.bend` (`Bv`: the sidebar's cats and rooms under the projects, a bot's header and tabs, rooms, remote bots; `Bv.frame` wraps `Lay.frame.x`, whose hooks add to the sidebar and replace the main area), `src/app/space.bend` (`Spv`: a bot's space blocks drawn natively), `src/app/forms.bend` (`Fm`: clipped chips and form fields whose text is the client's `@f.` scratch; nui.bend's `Fld` edits them, focus id `Fld.id(name, enter action, value prefix)`), `src/app/cats.bend` (`Cats`: core/cat.bend's cats flattened once per look+mood into a cache kept in main.bend's `Kept`, posed per frame). Cat frames: 33 ms timer (main.bend's `Shell.cats`) only while `Bv.moving` (a moving mood, window focused: `@blur`, no lightbox). A bot's browser frames load as images keyed `shot::` (main.bend's `Shot`). Screens without a window: `build/bots_snap DIR` (`test/native/bots_snap.bend`). +- `src/core/far.bend`: threads elsewhere (pure: a linked machine's things named `~`, what a hub shares with the owner's other machines, batches taken once and in order with gaps pulled, a client's fold that never moves its own sequence, requests routed to the owning hub with the real reply preserved, the hub's feed, pushes and mirrors, history epochs and isolated projection resets; laws `far_*`). `src/server/server.bend`'s `Far.*`/`Hub.far.*` do the IO: `POST /far/push`, `/far/log`, `/far/rpc`, mirrors in `/far/.jsonl`. The owner shares projects and ordinary threads as well as bots. `Label.title` names their machine and availability in every UI; a mirrored bot uses the usual bot views, and `Far.arrived` adopts a reported-only choice when its thread arrives (native focus follows in `Nui.unremote`). Discovery offers a head hint and pulls authoritative history with `Far.returned` / `Far.offer`, so a quick owning-hub restart or a hint arriving before ownership recognition cannot strand an old mirror. Phones drop mirrored projects and active rows only once the paired owner has that entity (`Hubs.catalog.*`), and share the sidebar view preference across hubs; hidden fold counts are deduplicated and a paired owner keeps its machine badge. `Hubs.origin.*` also names a direct paired owner on Active rows and focused thread/bot headers, marking an offline socket away without relabeling an already-marked far view. See docs/bots.md "Threads elsewhere". Tests: `test/far_test.bend`, `bun test/tools/far_e2e.ts`; `test/native/far_bench.bend` measures real bot frames, and `test/native/link_bench.bend` times a window switch to another hub. +- `src/core/bothub.bend`'s `Dir.*` and `DirWork.*`: per-peer directory merges and a four-slot deduplicating refresh queue (laws `remote_dir_*`). `src/server/botnet.bend` uses two-second connect/four-second total deadlines and atomically caches the directory in `/bots.cache`; the hub publishes each peer as it arrives, restores cached rows away at startup, and refreshes on links and peer return. Tests: `test/botdir_test.bend`, `test/tools/botdir_e2e.ts`. - `src/core/subs.bend`: the Subagents panel (pure): the tasks a thread delegated and what Claude runs for it (Agent calls, background shells and monitors: model.bend's `Bg`, whose line keeps the call, what it was asked and its kind, `Bg.kind`; a shell's row reads "shell: …" and only subagents count as agents). A subagent's lines (`parent_tool_use_id`) go to its own log, `M.Bg.log(thread)` (`@sub:`, off the timeline; hub.bend's `Sub.*`), never the thread's messages. Rows at work say what they do now; a subagent at work shows its newest steps until shut (`fold` action, key `sub:`); the panel is outlined in the accent with "N at work" while any work, and a sidebar row says "N agents" where its age goes (`Subs.badge`). Window `Lay.subs`/`Sa.*` (above the timeline), web `View.subs` (sticky), phones `subs` (above the composer). Laws `subs_*`, `bg_start_keeps_call`; test `test/subagents_test.bend`. - `src/core/tools.bend`: the thread header's buttons, one list for all four clients (pure: each an icon, a label, a sentence for its tooltip, on/danger, and where a group starts; stop, then pin/settle/snooze/archive/delete, then fork thread/bring back, then terminal/panel; laws `tools_*`). The window draws them icon + label with a line between groups and drops to icons only when the title would be squeezed (`Lay.tools` in layout.bend; the terminal button is the window action `vw-key`); the web as `WIcon.button`s (`View.tools`); the phones' toolbar menus map the icon names to SF Symbols and Material icons (`icon(_:)` in Views.swift, `toolIcon` in ThreadParts.kt). The panel button is "Panel" (it holds Board, Schematic, 3D, Mech, Diff and Browser), so the header has no Diff button of its own. An agent starting in a KiCad project opens its thread's panel on the board (hub.bend's `Auto.view`, sent by server.bend's `Hub.auto` at each Claude, Codex or Grok start as a `view` message with `auto`); a client takes it only while that thread's viewer has shown nothing and its panel was never toggled by hand (client.bend's `Viewer.auto`, `panel.hand:`; laws `viewer_auto_*`, `auto_view_none`). - `src/core/once.bend`: lists that show each thing once (pure: items keyed by what they stand for, the first of each key kept; laws `once_distinct`, `once_keeps`). The bot lists key a bot here by its id and one elsewhere by its name at its machine's link id (`BU.key`, `BT.Member.key`): the sidebar (`BU.rows`), the room picker (`BU.picks`) and the phone over every paired hub (`Hubs.bots`); laws `bot_rows_once`, `bot_picks_once`, `hubs_bots_once`. Test: `test/botlist_test.bend`. @@ -134,6 +136,7 @@ Headless UI checks: start Xvfb on `:77`, run `DISPLAY=:77 BACKPLANE_SNAP=/tmp/sn - "an arity over 255" at `-o` (not at `--check-only`) means a value held in the app's state flattened into too many fields: a deep record type (the board face inside the viewer's `Vx`). Put the deep part behind a `List` (`Scene3.face`). Adding a field to main.bend's `Kept` did it too (the room's wrapped posts): `park` now sits behind a one-element List. - `Nat.read` (and `Maybe.map` over it) in anything a law normalizes makes `bend PROOF.bend` crawl for minutes. Parse digits yourself (`Switch.num` in model.bend, `Tag.num` in hub.bend), and time a new law alone before the full run. - `.` and `_` mangle alike in C: `TL.mine.of` and `TL.mine_of` both become `..._TL_MINE_OF`, and the native build stops with "two names mangle to". `--check-only` does not catch it. +- A hub started on a copy of a real `events.jsonl` (to measure with real data) resumes every turn that was running in it as a live agent in the real folders (`Hub.recover.carry`). Run such a hub with no `claude`/`codex`/`grok` on its PATH (stand-ins as in `test/tools/*_e2e.ts`), and with `--foreground` so its PID is yours. - Launch time: nothing slow may run before the window opens (probes, network, full log copies). Start it after, in the background, and serve the last answer meanwhile (`src/core/boot.bend`). `test/native/boot_bench.bend` times start-up and frames on a real log. - CPU forks are dealt out once, not stolen: fork over one flat balanced tree of work items (all primitives of all nodes) rather than nesting forks per group; nested forks inside a heavy group barely spread. - The runtime deals out a value's work at the first fork it meets while evaluating it, and everything after runs where it was dealt. A spatial quadtree whose busy part sits in one corner (a thread's text) ran on one core however many there were. Collect the heavy items into a list, deal them round robin into a balanced tree (`Jobs.deal` in tiles.bend), and evaluate that tree in its own `do` binding so its forks are the first ones (main.bend's `Shell.present`). `test/native/par_bench.bend` shows each shape on 1 and 16 cores. diff --git a/LAWS.bend b/LAWS.bend index 769f635ea4..c70ee17716 100644 --- a/LAWS.bend +++ b/LAWS.bend @@ -1,6 +1,7 @@ import Base import ./src/core/bothub.bend as BHS import ./src/core/bot.bend as BOT +import ./src/core/far.bend as FR import ./src/core/botui.bend as BU import ./src/core/model.bend as M import ./src/app/viewer.bend as V @@ -3406,6 +3407,481 @@ law bots_moving_cat_follows_clock: for +c: Nat {BV.Bv.warm.at("work", c) == c : Nat} +# Threads elsewhere (far.bend) +# ---------------------------- +# A thing on a linked machine is named by the link and its own id there, +# so it never takes the place of anything here, and a request about it +# goes back to that machine in that machine's ids. + +law far_id_parts: + {FR.Fr.link(FR.Fr.id("p1", "t1")) ++ " " ++ FR.Fr.local(FR.Fr.id("p1", "t1")) == "p1 t1" : String} + +# an id here names nothing elsewhere and is its own local id +law far_id_here: + {FR.Fr.link("t1790715007-34164") ++ "|" ++ FR.Fr.local("t1790715007-34164") == "|t1790715007-34164" : String} + +# a linked machine's message lands in its thread as named here, never in +# a thread of this machine +law far_change_names_link: + for +l: String + for +th: String + for +msg: String + for role: M.Role + for +text: String + for +at: Nat + {FR.Fr.change(l, M.MessagePosted{th, msg, role, text, at}) == Some{M.MessagePosted{FR.Fr.id(l, th), msg, role, text, at}} : Maybe<&2, M.Change>} + +law far_change_bot_named: + for +l: String + for +id: String + for +name: String + for +th: String + for +look: Nat + for +persona: String + for +at: Nat + {FR.Fr.change(l, M.BotSet{id, name, th, look, persona, at}) == Some{M.BotSet{FR.Fr.id(l, id), name, FR.Fr.id(l, th), look, persona, at}} : Maybe<&2, M.Change>} + +# settings, links, devices, webhooks and rooms stay on their machine: never +# shared, never folded in elsewhere +law far_settings_stay_home: + for +s: M.State + for +k: String + for +v: String + for +at: Nat + {Bool.or(FR.Fr.shared(s, M.SettingSet{k, v, at}), FR.Fr.is.some(FR.Fr.change("p1", M.SettingSet{k, v, at}))) == False{} : Bool} + +law far_peers_stay_home: + for +s: M.State + for +i: String + for +n: String + for +u: String + for r: Bool + for +at: Nat + {Bool.or(FR.Fr.shared(s, M.PeerSet{i, n, u, r, at}), FR.Fr.is.some(FR.Fr.change("p1", M.PeerSet{i, n, u, r, at}))) == False{} : Bool} + +law far_hooks_stay_home: + for +s: M.State + for +i: String + for +b: String + for +n: String + for r: Bool + for +at: Nat + {Bool.or(FR.Fr.shared(s, M.HookSet{i, b, n, r, at}), FR.Fr.is.some(FR.Fr.change("p1", M.HookSet{i, b, n, r, at}))) == False{} : Bool} + +law far_devices_stay_home: + for +s: M.State + for +tk: String + for +pl: String + for +k: String + for +th: String + for +env: String + for +bu: String + for +at: Nat + {Bool.or(FR.Fr.shared(s, M.DeviceRegistered{tk, pl, k, th, env, bu, at}), FR.Fr.is.some(FR.Fr.change("p1", M.DeviceRegistered{tk, pl, k, th, env, bu, at}))) == False{} : Bool} + +law far_rooms_stay_home: + for +s: M.State + for +i: String + for +r: String + for +f: String + for +t: String + for +h: Nat + for +at: Nat + {Bool.or(FR.Fr.shared(s, M.RoomPosted{i, r, f, t, h, at}), FR.Fr.is.some(FR.Fr.change("p1", M.RoomPosted{i, r, f, t, h, at}))) == False{} : Bool} + +# a batch past a gap takes nothing (the rest is asked for), whatever it holds +law far_gap_takes_nothing: + for xs: List<&2, FR.Item> + {FR.Fr.take(3n, 5n, 9n, xs) == FR.Got{3n, Nil{}, True{}} : FR.Got} + +# a batch already had (late, or sent twice) takes nothing and moves nothing back +law far_old_takes_nothing: + for a: J.Json + for b: J.Json + {FR.Fr.take(9n, 3n, 6n, [FR.Item{4n, a}, FR.Item{6n, b}]) == FR.Got{9n, Nil{}, False{}} : FR.Got} + +# of a batch that overlaps what is had, only what is new is taken +law far_overlap_takes_new: + for a: J.Json + for b: J.Json + for c: J.Json + {FR.Fr.take(5n, 3n, 8n, [FR.Item{4n, a}, FR.Item{6n, b}, FR.Item{8n, c}]) == FR.Got{8n, [FR.Item{6n, b}, FR.Item{8n, c}], False{}} : FR.Got} + +# and the same batch again then takes nothing +law far_batch_again: + for a: J.Json + for b: J.Json + {FR.Fr.take(8n, 5n, 8n, [FR.Item{6n, a}, FR.Item{8n, b}]) == FR.Got{8n, Nil{}, False{}} : FR.Got} + +# a request about a thread here is never sent elsewhere; one about a +# thread elsewhere goes to its link +law far_route_here_stays: + {FR.Fr.route(J.Json.obj([J.KV{"m", J.Str{"turn.start"}}, J.KV{"p", J.Json.obj([J.KV{"thread", J.Str{"t1"}}, J.KV{"text", J.Str{"a~b"}}])}])) == None{} : Maybe<&2, FR.Route>} + +law far_route_goes_home: + {FR.Route.link.of(FR.Fr.route(J.Json.obj([J.KV{"m", J.Str{"turn.interrupt"}}, J.KV{"p", J.Json.obj([J.KV{"thread", J.Str{"p1~t1"}}])}]))) == "p1" : String} + +# the owning hub takes from a linked machine only what Fr.methods lists +law far_allowed_only_methods: + for +s: M.State + for +p: J.Json + {FR.Fr.allowed(s, J.Json.obj([J.KV{"m", J.Str{"setting.set"}}, J.KV{"p", p}])) == False{} : Bool} + +law far_no_delete: + for +s: M.State + for +p: J.Json + {FR.Fr.allowed(s, J.Json.obj([J.KV{"m", J.Str{"thread.delete"}}, J.KV{"p", p}])) == False{} : Bool} + +# where a thing lives is shown beside its name, only for a thing elsewhere +law far_where_here: + for +n: String + {FR.Fr.where("t1", n) == "" : String} + +law far_where_far: + {FR.Fr.where("p1~t1", "box") == " · box" : String} + +# a bot mirrored from a linked machine has only the tabs its machine +# shares; a bot here keeps them all +law far_bot_tabs: + {BU.BU.tabs.of("p1~b1") == ["chat", "space", "memory", "routines"] : List<&2, String>} + +law far_bot_tabs_here: + {BU.BU.tabs.of("b1") == BU.BU.tabs() : List<&2, String>} + +# Remote bots in the same view +# ---------------------------- +# A row opens what it stands for: a bot with a thread here (this +# machine's, or one mirrored from a linked machine) as a bot; a bot only +# reported by a linked machine as the conversation with it +law far_row_act_mirror: + for +i: String + for +n: String + for +lk: Nat + for +m: String + for +no: String + for +se: Bool + for +pe: String + for +l: String + {BU.BU.act(BU.BRow{i, n, "p1~bt1", lk, m, no, se, True{}, pe, l}) == "bot" : String} + +law far_row_act_reported: + for +i: String + for +n: String + for +lk: Nat + for +m: String + for +no: String + for +se: Bool + for +pe: String + for +l: String + {BU.BU.act(BU.BRow{i, n, "", lk, m, no, se, True{}, pe, l}) == "remote" : String} + +# a bot elsewhere is named with its machine; one here by its name alone +law bot_row_title_far: + for +i: String + for +th: String + for +lk: Nat + for +m: String + for +no: String + for +se: Bool + for +l: String + {BU.BU.row.title(BU.BRow{i, "Kit", th, lk, m, no, se, True{}, "box", l}) == "Kit · box" : String} + +law bot_row_title_here: + for +i: String + for +n: String + for +th: String + for +lk: Nat + for +m: String + for +no: String + for +se: Bool + for +pe: String + for +l: String + {BU.BU.row.title(BU.BRow{i, n, th, lk, m, no, se, False{}, pe, l}) == n : String} + +# a bot only reported by a linked machine shows in a bot's view with one +# tab, its chat, and nothing that acts on its machine +law bot_head_remote: + for +u: U.Ui + for +id: String + for +t: String + for +lk: Nat + for +m: String + {BU.BU.head.m(True{}, u, id, t, lk, m) == BU.Head{t, lk, m, BU.Mood.note(m), ["chat"], "chat", False{}, False{}} : BU.Head} + +law bot_head_remote_view: + for +u: U.Ui + for +id: String + {BU.Head.tabs(BU.BU.head(u, BU.VRemote{id})) == ["chat"] : List<&2, String>} + +# a mirrored bot's view has the tabs its machine shares, no viewer (its +# files are on its machine), and its memory and routines are read here +law far_bot_head_tabs: + for +u: U.Ui + for +t: String + for +lk: Nat + for +m: String + {BU.Head.tabs(BU.BU.head.m(False{}, u, "p1~b1", t, lk, m)) == ["chat", "space", "memory", "routines"] : List<&2, String>} + +law far_bot_head_stays_home: + for +u: U.Ui + for +t: String + for +lk: Nat + for +m: String + {Bool.or(BU.Head.viewer(BU.BU.head.m(False{}, u, "p1~b1", t, lk, m)), BU.Head.owns(BU.BU.head.m(False{}, u, "p1~b1", t, lk, m))) == False{} : Bool} + +law bot_head_here_owns: + for +u: U.Ui + for +t: String + for +lk: Nat + for +m: String + {Bool.and(BU.Head.viewer(BU.BU.head.m(False{}, u, "b1", t, lk, m)), BU.Head.owns(BU.BU.head.m(False{}, u, "b1", t, lk, m))) == True{} : Bool} + +# on a phone paired with a bot's own hub and a hub that mirrors it, the +# mirrored copy adds nothing: the bot shows once, as its own hub's +law hubs_bots_mirror_dropped: + for +far: Bool + for +want: Bool + for +hs: List<&2, Hb.Hub> + for +from: String + for +r: J.Json + for rest: List<&2, ON.Ky> + {Hb.Hubs.bot.keep.k(True{}, far, want, hs, from, r, rest) == rest : List<&2, ON.Ky>} +# Text fitting: early exit preserves the original recurrence. + +law fit_count_guard: + for +s: String + for +fits: Bool + for +cache: Map<&2, X.TG> + for +f: X.Face + for +max: U32 + for +used: U32 + {X.Fit.count.go(s, fits, cache, f, max, used) == X.Fit.count.k(fits, X.Fit.count(s, cache, f, max, used)) : Nat} + +law fit_count_step: + for +cp: U32 + for +s: String + for +cache: Map<&2, X.TG> + for +f: X.Face + for +max: U32 + for +used: U32 + {X.Fit.count(SCon{Chr{cp}, s}, cache, f, max, used) == + X.Fit.count.k(U32.is_le((used + X.TG.adv(X.Text.get(cache, f, cp)) : U32), max), + X.Fit.count(s, cache, f, max, (used + X.TG.adv(X.Text.get(cache, f, cp)) : U32))) : Nat} + +law fit_chunk_progress: + for n: Nat + exs k: Nat + {X.Fit.atleast(n) == 1n+k : Nat} + +law fit_wrap_unicode: + {X.Text.wrap(Map.new(&2, X.TG), X.Text.bitmap(), "日本語 😀🚀🙂\nabcde x\n", 24) == + ["日本語", "😀🚀🙂", "abc", "de ", "x", ""] : List<&2, String>} +# Per-peer remote bot directories and bounded refreshes + +law remote_dir_failed_peer: + for +xs: List<&2, J.Json> + for +p: String + {BHS.Dir.merge(None{}, xs, p) == BHS.Dir.away(xs, p) : List<&2, J.Json>} + +law remote_dir_away_seen: + for +p: String + for +n: String + for +look: Nat + for +seen: Nat + {J.Json.get_obj(BHS.Dir.away.one(BHS.Dir.item(p, "machine", look, n, "work", seen)), "seen") == J.Json.nat(seen) : J.Json} + +law remote_dir_arrival_order: + {BHS.Dir.merge(Some{[BHS.Dir.item("a", "alpha", 0n, "miso", "ready", 1n)]}, + [BHS.Dir.item("b", "beta", 0n, "nori", "ready", 1n)], "a") == + BHS.Dir.merge(Some{[BHS.Dir.item("b", "beta", 0n, "nori", "ready", 1n)]}, + [BHS.Dir.item("a", "alpha", 0n, "miso", "ready", 1n)], "b") : List<&2, J.Json>} + +law remote_dir_other_unchanged: + for +o: J.Json + {BHS.Dir.changed(False{}, o) == o : J.Json} + +law remote_dir_obsolete_ignored: + for +xs: List<&2, J.Json> + for +p: String + for +r: Maybe<&2, List<&2, J.Json>> + {BHS.Dir.accept(False{}, r, xs, p) == xs : List<&2, J.Json>} + +law remote_dir_known_not_queued: + for +a: List<&2, String> + for +q: List<&2, String> + for +p: String + {BHS.DirWork.add.k(True{}, a, q, p) == BHS.DirWork{a, q} : BHS.DirWork} + +law remote_dir_four_slots: + {BHS.DirPlan.launch(BHS.DirWork.plan(BHS.DirWork{[], ["a", "b", "c", "d", "e"]})) == ["a", "b", "c", "d"] : List<&2, String>} + +law remote_dir_freed_slot: + {BHS.DirPlan.launch(BHS.DirWork.plan(BHS.DirWork.finish(BHS.DirWork{["d", "c", "b", "a"], ["e"]}, "b"))) == ["e"] : List<&2, String>} + +# A pending selection cannot consume another hub's read model. +law want_other_hub: + for +u: U.Ui + for +generation: Bool + for +installed: Bool + {U.Bots.wanted.k(U.Want.accept(False{}, generation, installed), u) == u : U.Ui} + +law want_old_generation: + for +u: U.Ui + for +hub: Bool + for +installed: Bool + {U.Bots.wanted.k(U.Want.accept(hub, False{}, installed), u) == u : U.Ui} + +law want_before_install: + for +u: U.Ui + for +hub: Bool + for +generation: Bool + {U.Bots.wanted.k(U.Want.accept(hub, generation, False{}), u) == u : U.Ui} + +law machine_clears_selection: + for +u: U.Ui + for +hub: String + {U.Ui.sel(U.Act.ui(U.Ui.machine(u, hub))) == "" : String} + +law timeline_hidden_settings: + for +selected: Bool + for +thread: Bool + {U.Timeline.visible(True{}, selected, thread) == False{} : Bool} + +law timeline_hidden_pane: + for +settings: Bool + for +selected: Bool + {U.Timeline.visible(settings, selected, False{}) == False{} : Bool} + +# Cache walks return at most one home and one linked snapshot. +law hub_cache_home_short: + for +vs: List<&2, U.HubView> + {List.drop(&2, U.HubView, U.HubCache.home(vs), 1n) == Nil{} : List<&2, U.HubView>} + +law hub_cache_remote_short: + for +vs: List<&2, U.HubView> + {List.drop(&2, U.HubView, U.HubCache.remote(vs), 1n) == Nil{} : List<&2, U.HubView>} + +law hub_cache_bounded: + for +v: U.HubView + for +vs: List<&2, U.HubView> + {List.drop(&2, U.HubView, U.HubCache.put(v, vs), 2n) == Nil{} : List<&2, U.HubView>} + +# Owner-shared projects and ordinary threads use the far projection too. +law far_project_named: + for +l: String + for +id: String + for title: String + for root: String + for at: Nat + {FR.Fr.change(l, M.ProjectCreated{id, title, root, at}) == Some{M.ProjectCreated{FR.Fr.id(l, id), title, root, at}} : Maybe<&2, M.Change>} + +law far_project_shared: + for s: M.State + {FR.Fr.shared(s, M.ProjectCreated{"p1", "Board", "/board", 1n}) == True{} : Bool} + +law far_project_thread_shared: + {FR.Fr.thread.shared(M.State.replay([M.ProjectCreated{"p", "Board", "/board", 1n}, + M.ThreadCreated{"t", "p", "Layout", M.Local{}, "", "", "claude", "", 2n}], M.State.empty()), "t") == True{} : Bool} + +law far_label_here: + for +s: M.State + for +id: String + for title: String + {FR.Fr.title.k(False{}, s, id, title) == title : String} + +law far_label_away: + {FR.Fr.title(FR.Fr.mark(M.State.empty(), "p1", "desk", True{}), "p1~t", "Layout") == "Layout · desk (away)" : String} + +law far_cursor_keeps_marker: + {FR.Fr.title(FR.Fr.head.set(FR.Fr.mark(M.State.empty(), "p1", "desk", True{}), "p1", 5n), "p1~t", "Layout") == "Layout · desk (away)" : String} + +law far_routine_routes_home: + {FR.Fr.params.local(J.Field{"routine", J.Str{"p1~r1"}, J.End{}}, "p1") == J.Field{"routine", J.Str{"r1"}, J.End{}} : J.Json} + +law hubs_catalog_mirror_dropped: + for r: J.Json + for rest: List<&2, J.Json> + {Hb.Hubs.part.k(Bool.not(True{}), r, rest) == rest : List<&2, J.Json>} + +law hubs_sidebar_view_everywhere: + {Hb.Hubs.every.op(Hb.Do{"setting", "sidebar.view=active"}) == True{} : Bool} + +# History reset and restart races remain isolated to their owner. +law far_epoch_legacy: + for +old: String + {FR.Fr.epoch.changed(old, "") == False{} : Bool} + +law far_epoch_same: + {FR.Fr.epoch.changed("history1", "history1") == False{} : Bool} + +law far_epoch_reset: + {FR.Fr.epoch.changed("history1", "history2") == True{} : Bool} + +law far_reset_keeps_sequence: + for s: M.State + for +l: String + {M.State.seq(FR.Fr.forget(s, l)) == M.State.seq(s) : Nat} + +law far_loaded_live_wins: + {FR.In.head(FR.In.loaded(FR.In.put(FR.In.new("p1", "desk"), 0n, 9n, Nil{}), + FR.In.new("p1", "desk"))) == 9n : Nat} + +law far_reply_preserves_error: + {FR.Fr.reply(J.Json.obj([J.KV{"id", J.Num{"0"}}, J.KV{"ok", J.Flag{False{}}}, J.KV{"error", J.Str{"declined"}}]), "7") == + "{\"id\":7,\"ok\":false,\"error\":\"declined\"}" : String} + +law far_draft_kept: + for text: String + {U.Draft.keep("p1~t1", text) == [U.Keep{"p1~t1", text}] : List<&2, U.Cmd>} + +law hubs_fold_null: + for +older: Bool + for +hs: List<&2, Hb.Hub> + for +from: String + {Hb.Hubs.fold.json(J.Null{}, older, hs, from) == J.Null{} : J.Json} + +law hubs_origin_here: + for p: J.Json + for name: String + {Hb.Hubs.origin.project(False{}, p, name) == p : J.Json} + +law hubs_origin_elsewhere: + {J.Json.get_str(Hb.Hubs.origin.project(True{}, J.Json.obj([J.KV{"title", J.Str{"Board"}}]), "desk"), "machine", "") == "desk" : String} + +law far_arrival_no_choice: + for u: U.Ui + {U.Far.arrived.k(False{}, u) == u : U.Ui} + +law far_arrival_not_shared: + for u: U.Ui + {U.Far.arrived.m(None{}, u) == u : U.Ui} + +law far_arrival_thread_selected: + for u: U.Ui + for id: String + for name: String + for +thread: String + {U.Ui.sel(U.Far.arrived.m(Some{BOT.Bot{id, name, thread, 1n, "", 0n, False{}}}, u)) == thread : String} + +law far_arrival_focus_unchanged: + for n: N.Nui + {N.Nui.unremote.k(False{}, n) == n : N.Nui} + +law hubs_origin_label_plain: + for v: J.Json + for name: String + for online: Bool + {Hb.Hubs.origin.label(False{}, v, name, online) == v : J.Json} + +law hubs_origin_title_here: + for title: String + for name: String + {Hb.Hubs.origin.title(True{}, title, name, True{}) == title ++ " · " ++ name : String} + +law hubs_origin_title_away: + for title: String + for name: String + {Hb.Hubs.origin.title(True{}, title, name, False{}) == title ++ " · " ++ name ++ " (away)" : String} # a subagent started by an Agent call keeps the call and what it was asked # while its progress changes what it does law bg_start_keeps_call: diff --git a/PROOF.bend b/PROOF.bend index 9560d96947..1a22783208 100644 --- a/PROOF.bend +++ b/PROOF.bend @@ -1,4 +1,5 @@ import Base +import ./src/gfx/text.bend as X import ./src/core/bothub.bend as BHS import ./src/core/bot.bend as BOT import ./src/core/botui.bend as BU @@ -4654,10 +4655,54 @@ def Laws.window_state_seq(s): case M.State{ps, ts, es, n, tk, sg, qu, ak, dv, nv, bw}: {==} -def Laws.window_adopt(u, st): +def reselect_state(same: Bool, +u: U.Ui, +old: String) -> {U.Ui.state(U.Ui.reselect.k(same, u, old)) == U.Ui.state(u) : M.State}: + match same u: + case True{} _: {==} + case False{} U.Ui{st, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr}: {==} + +def wanted_bot_state(m: Maybe<&2, BOT.Bot>, +u: U.Ui) -> {U.Ui.state(U.Bots.wanted.m(m, u)) == U.Ui.state(u) : M.State}: + match m u: + case Some{BOT.Bot{i, n, th, lk, p, c, d}} U.Ui{st, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr}: {==} + case None{} _: {==} + +def wanted_thread_state(found: Bool, +u: U.Ui, +id: String) -> {U.Ui.state(U.Want.thread.k(found, u, id)) == U.Ui.state(u) : M.State}: + match found u: + case True{} U.Ui{st, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr}: {==} + case False{} _: {==} + +def wanted_state(bound: Bool, +u: U.Ui) -> {U.Ui.state(U.Bots.wanted.k(bound, u)) == U.Ui.state(u) : M.State}: + match bound: + case False{}: {==} + case True{}: + +m = BOT.World.find(M.State.bots(U.Ui.state.of(u)), U.Ui.scratch(u, "@bot.want")) + +found = Bool.and(Bool.not(String.is_empty(U.Ui.scratch(u, "@thread.want"))), U.Want.thread.exists(U.Ui.state.of(u), U.Ui.scratch(u, "@thread.want"))) + +id = U.Ui.scratch(u, "@thread.want") + Equal.trans(M.State, U.Ui.state(U.Want.thread.k(found, U.Bots.wanted.m(m, u), id)), U.Ui.state(U.Bots.wanted.m(m, u)), U.Ui.state(u), + wanted_thread_state(found, U.Bots.wanted.m(m, u), id), wanted_bot_state(m, u)) + +def wanted_any_state(pending: Bool, +u: U.Ui) -> {U.Ui.state(U.Bots.wanted.any(pending, u)) == U.Ui.state(u) : M.State}: + match pending: + case False{}: {==} + case True{}: wanted_state(U.Want.bound(u), u) + +def installed_state(+u: U.Ui) -> {U.Ui.state(U.Ui.installed(u)) == U.Ui.state(u) : M.State}: match u: - case U.Ui{s0, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr}: - {==} + case U.Ui{st, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr}: {==} + +def adopt_raw_state(+u: U.Ui, +st: M.State) -> {U.Ui.state(U.Ui.adopt.raw(u, st)) == st : M.State}: + match u: + case U.Ui{s0, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr}: {==} + +def Laws.window_adopt(u, st): + match u st: + case +uu +ss: + +raw = U.Ui.adopt.raw(uu, ss) + +ins = U.Ui.installed(raw) + +wanted = U.Bots.wanted(ins) + Equal.trans(M.State, U.Ui.state(U.Ui.reselect(U.Ui.sel(uu), wanted)), U.Ui.state(wanted), ss, + reselect_state(String.eq(U.Ui.sel(uu), U.Ui.sel(wanted)), wanted, U.Ui.sel(uu)), + Equal.trans(M.State, U.Ui.state(wanted), U.Ui.state(ins), ss, wanted_any_state(Bool.or(Bool.not(String.is_empty(U.Ui.scratch(ins, "@bot.want"))), Bool.not(String.is_empty(U.Ui.scratch(ins, "@thread.want")))), ins), + Equal.trans(M.State, U.Ui.state(ins), U.Ui.state(raw), ss, installed_state(raw), adopt_raw_state(uu, ss)))) def Laws.todo_write_whole_list(): {==} @@ -5782,6 +5827,301 @@ def Laws.bots_away_cat_posed_once(c): def Laws.bots_moving_cat_follows_clock(c): {==} +def Laws.far_id_parts(): + {==} + +def Laws.far_id_here(): + {==} + +def Laws.far_change_names_link(l, th, msg, role, text, at): + {==} + +def Laws.far_change_bot_named(l, id, name, th, look, persona, at): + {==} + +def Laws.far_settings_stay_home(s, k, v, at): + {==} + +def Laws.far_peers_stay_home(s, i, n, u, r, at): + {==} + +def Laws.far_hooks_stay_home(s, i, b, n, r, at): + {==} + +def Laws.far_devices_stay_home(s, tk, pl, k, th, env, bu, at): + {==} + +def Laws.far_rooms_stay_home(s, i, r, f, t, h, at): + {==} + +def Laws.far_gap_takes_nothing(xs): + {==} + +def Laws.far_old_takes_nothing(a, b): + {==} + +def Laws.far_overlap_takes_new(a, b, c): + {==} + +def Laws.far_batch_again(a, b): + {==} + +def Laws.far_route_here_stays(): + {==} + +def Laws.far_route_goes_home(): + {==} + +def Laws.far_allowed_only_methods(s, p): + {==} + +def Laws.far_no_delete(s, p): + {==} + +def Laws.far_where_here(n): + {==} + +def Laws.far_where_far(): + {==} + +def Laws.far_bot_tabs(): + {==} + +def Laws.far_bot_tabs_here(): + {==} + +def Laws.far_row_act_mirror(i, n, lk, m, no, se, pe, l): + {==} + +def Laws.far_row_act_reported(i, n, lk, m, no, se, pe, l): + {==} + +def Laws.bot_row_title_far(i, th, lk, m, no, se, l): + {==} + +def Laws.bot_row_title_here(i, n, th, lk, m, no, se, pe, l): + {==} + +def Laws.bot_head_remote(u, id, t, lk, m): + {==} + +def Laws.bot_head_remote_view(u, id): + {==} + +def Laws.far_bot_head_tabs(u, t, lk, m): + {==} + +def Laws.far_bot_head_stays_home(u, t, lk, m): + {==} + +def Laws.bot_head_here_owns(u, t, lk, m): + {==} + +def Laws.hubs_bots_mirror_dropped(far, want, hs, from, r, rest): + {==} + +# Text fitting: early exit preserves the original recurrence. + +def Laws.fit_count_guard(s, fits, cache, f, max, used): + match s fits: + case SNil{} False{}: + {==} + case SCon{Chr{cp}, t} False{}: + {==} + case SNil{} True{}: + {==} + case SCon{Chr{cp}, t} True{}: + {==} + +def Laws.fit_count_step(cp, s, cache, f, max, used): + Laws.fit_count_guard(s, U32.is_le((used + X.TG.adv(X.Text.get(cache, f, cp)) : U32), max), + cache, f, max, (used + X.TG.adv(X.Text.get(cache, f, cp)) : U32)) + +def Laws.fit_chunk_progress(n): + match n: + case 0n: + (0n, {==}) + case 1n+k: + (k, {==}) + +def Laws.fit_wrap_unicode(): + {==} + +# Per-peer remote bot directories and bounded refreshes +def Laws.remote_dir_failed_peer(xs, p): + {==} + +def Laws.remote_dir_away_seen(p, n, look, seen): + {==} + +def Laws.remote_dir_arrival_order(): + {==} + +def Laws.remote_dir_other_unchanged(o): + {==} + +def Laws.remote_dir_obsolete_ignored(xs, p, r): + {==} + +def Laws.remote_dir_known_not_queued(a, q, p): + {==} + +def Laws.remote_dir_four_slots(): + {==} + +def Laws.remote_dir_freed_slot(): + {==} + +def Laws.want_other_hub(u, generation, installed): + match generation installed: + case True{} True{}: {==} + case True{} False{}: {==} + case False{} True{}: {==} + case False{} False{}: {==} + +def Laws.want_old_generation(u, hub, installed): + match hub installed: + case True{} True{}: {==} + case True{} False{}: {==} + case False{} True{}: {==} + case False{} False{}: {==} + +def Laws.want_before_install(u, hub, generation): + match hub generation: + case True{} True{}: {==} + case True{} False{}: {==} + case False{} True{}: {==} + case False{} False{}: {==} + +def Laws.machine_clears_selection(u, hub): + match u: + case U.Ui{_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}: {==} + +def Laws.timeline_hidden_settings(selected, thread): + match selected thread: + case True{} True{}: {==} + case True{} False{}: {==} + case False{} True{}: {==} + case False{} False{}: {==} + +def Laws.timeline_hidden_pane(settings, selected): + match settings selected: + case True{} True{}: {==} + case True{} False{}: {==} + case False{} True{}: {==} + case False{} False{}: {==} + +def home_short_step(home: Bool, +v: U.HubView, +rest: List<&2, U.HubView>, p: {List.drop(&2, U.HubView, rest, 1n) == Nil{} : List<&2, U.HubView>}) -> {List.drop(&2, U.HubView, U.HubCache.home.k(home, v, rest), 1n) == Nil{} : List<&2, U.HubView>}: + match home: + case True{}: {==} + case False{}: p + +def Laws.hub_cache_home_short(vs): + match vs: + case Nil{}: {==} + case +v <> +t: + home_short_step(String.is_empty(U.HubView.hub(v)), v, U.HubCache.home(t), Laws.hub_cache_home_short(t)) + +def remote_short_step(home: Bool, +v: U.HubView, +rest: List<&2, U.HubView>, p: {List.drop(&2, U.HubView, rest, 1n) == Nil{} : List<&2, U.HubView>}) -> {List.drop(&2, U.HubView, U.HubCache.remote.k(home, v, rest), 1n) == Nil{} : List<&2, U.HubView>}: + match home: + case True{}: p + case False{}: {==} + +def Laws.hub_cache_remote_short(vs): + match vs: + case Nil{}: {==} + case +v <> +t: + remote_short_step(String.is_empty(U.HubView.hub(v)), v, U.HubCache.remote(t), Laws.hub_cache_remote_short(t)) + +def cache_bounded_step(home: Bool, +v: U.HubView, +vs: List<&2, U.HubView>) -> {List.drop(&2, U.HubView, U.HubCache.put.k(home, v, vs), 2n) == Nil{} : List<&2, U.HubView>}: + match home: + case True{}: Laws.hub_cache_remote_short(vs) + case False{}: Laws.hub_cache_home_short(vs) + +def Laws.hub_cache_bounded(v, vs): + cache_bounded_step(String.is_empty(U.HubView.hub(v)), v, vs) + +def Laws.far_project_named(l, id, title, root, at): + {==} + +def Laws.far_project_shared(s): + {==} + +def Laws.far_project_thread_shared(): + {==} + +def Laws.far_label_here(s, id, title): + {==} + +def Laws.far_label_away(): + {==} + +def Laws.far_cursor_keeps_marker(): + {==} + +def Laws.far_routine_routes_home(): + {==} + +def Laws.hubs_catalog_mirror_dropped(r, rest): + {==} + +def Laws.hubs_sidebar_view_everywhere(): + {==} + +def Laws.far_epoch_legacy(old): + {==} + +def Laws.far_epoch_same(): + {==} + +def Laws.far_epoch_reset(): + {==} + +def Laws.far_reset_keeps_sequence(s, l): + match s: + case M.State{_, _, _, _, _, _, _, _, _, _, _}: + {==} + +def Laws.far_loaded_live_wins(): + {==} + +def Laws.far_reply_preserves_error(): + {==} + +def Laws.far_draft_kept(text): + {==} + +def Laws.hubs_fold_null(older, hs, from): + {==} + +def Laws.hubs_origin_here(p, name): + {==} + +def Laws.hubs_origin_elsewhere(): + {==} + +def Laws.far_arrival_no_choice(u): + {==} + +def Laws.far_arrival_not_shared(u): + {==} + +def Laws.far_arrival_thread_selected(u, id, name, thread): + match u: + case U.Ui{_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}: + {==} + +def Laws.far_arrival_focus_unchanged(n): + {==} + +def Laws.hubs_origin_label_plain(v, name, online): + {==} + +def Laws.hubs_origin_title_here(title, name): + {==} + +def Laws.hubs_origin_title_away(title, name): + {==} def Laws.bg_start_keeps_call(): {==} diff --git a/docs/bots.md b/docs/bots.md index 8ec0e7944e..2a2afb2905 100644 --- a/docs/bots.md +++ b/docs/bots.md @@ -451,6 +451,7 @@ tools). Routes: | `POST /bots/deliver` | a linked machine | `X-Backplane-Peer` + signature with its secret, replay cache | | `POST /bots/link` | the holder of an invite | the same; once per peer id | | `GET /bots/dir` | a linked machine | the same, over the empty body | +| `POST /far/push`, `/far/log`, `/far/rpc` | one of the owner's linked machines | the same | | `GET /bots//screen.jpg` | clients | loopback, or the pairing token | | `GET /oauth/google` | the user's browser | loopback, or the pairing token; Google's state | @@ -482,7 +483,93 @@ as its own hub's. A bot elsewhere that lives on one of the owner's own machines (the machine switcher's list: the same address, or, when the link's address is on the owner's tailnet domain, the peer's name or MagicDNS label on the -same port, `Far.machine` in client.bend) opens there: the -window switches to that hub and shows the bot with all its tabs; the web -goes to that hub's page with `#bot=`. A bot on anyone else's machine, -and every bot elsewhere on the phones, is a conversation (`bots.tell`). +same port, `Far.machine` in client.bend) is mirrored here (below) and +opens here, at once, as a bot like any other. A bot on anyone else's +machine, or one not mirrored yet, is a conversation (`bots.tell`). The +window no longer switches hubs to show a bot: that fetched the other hub's +whole log over the link (11 to 20 s for a 13 MB log, `test/native/link_bench.bend`). + +## Threads elsewhere + +`src/core/far.bend`. A hub shares its projects, ordinary project threads and bots (turns, +messages, tool calls, asks, delegated tasks, bot space, memory and routines) with the owner's other machines it is +linked to. It shares no settings, devices, links, webhooks or rooms (rooms +travel on their own, above), and nothing with a machine that is not the +owner's (`Far.own` in server.bend: the machines list; laws `far_*_stay_home`). + +- Names. A thing on the machine behind link `L` is `L~` here + (`Fr.id`; local ids never hold a `~`). So nothing from elsewhere can take + the place of anything here, a bot shows once (its mirror and the + directory's report of it share `BU.key`, name@link; law `bot_rows_once`), + and a phone paired with both machines keeps the owner's own. +- Push. The owning hub is the authority. Each commit's shared changes go, + numbered by its log, to each such peer: `POST /far/push` + `{"name", "epoch", "since", "head", "items": [{"n", "c"}]}`, one push out per link + at a time, later batches merged behind it (`Out.*`). A batch says it + follows the last push that got through only while every push since did; + after a failure or a restart it says it follows only its own start. +- Pull. A mirror takes a batch only when it follows what it has + (`since <= head`) and only the items it lacks (`n > head`); a batch past a + gap takes nothing and the rest is pulled: `POST /far/log {"since", "epoch"}` + answers a page of about 400 KB `{"since", "epoch", "head", "items", "more"}` from the + owning hub's feed (its shared lines, made from the log off the hub at + start, then grown by each commit). A batch that comes twice, late or out + of order changes nothing (laws `far_gap_takes_nothing`, + `far_old_takes_nothing`, `far_overlap_takes_new`, `far_batch_again`). +- History identity. The epoch is a cached hash of the first durable wire + change. It stays the same through restart; replacing that first change + changes it. A pull with another epoch starts at zero. A push naming + another epoch triggers a pull from the authority (a delayed push cannot + revert history). Clients remove only that link's old projection, preserving + their local sequence and other mirrors. Old mirror files and epoch-free + messages remain readable. An exact copy of the original history prefix + has the same epoch; reset history must replace its first change. +- Mirrors. The receiving hub keeps one per link, in memory and in + `/far/.jsonl` (an epoch header, each batch's items, then `{"head"}`), and + sends it to each client that joins (`{"t":"far","link","name","away", + "epoch","since","head","items"}`, `since` 0) and each batch as it comes. A + client folds it into its read model under the far ids, never counting it + in its own log's sequence (a reconnect still asks its hub for exactly what + it missed), and keeps how far it has each link beside the read model, so + a fresh log forgets both at once (`Fr.fold`, `Ui.far`). +- Freshness and partitions. No polling of its own: pushes as changes + happen; a pull at start (after the mirror is read back from disk), on a + gap, and when the minute's directory exchange finds a link that was away + answering again. When discovery recognizes a linked owner machine, it + both offers the current head and pulls that peer's authoritative history + (`Far.returned`): an earlier hint may have arrived before this side + recognized ownership and been refused. This also checks a healthy cached + mirror after a quick restart. These are one-time discovery actions; the + existing minute directory and two-minute machine discovery remain the + only periodic checks. A link that does not answer the directory, or a pull, + is away: its bots and threads stay listed and readable, marked away + (`far.away.`, mood "away", "Kit · box (away)"). +- Requests. What a client does to a thread elsewhere (write, stop, answer + an ask, its queue, settle, a side question, its modes, a bot's space and + routines: `Fr.methods`) goes to its hub in that hub's ids (`Fr.route`, + `POST /far/rpc`), signed like every hub-to-hub request; the owning hub + takes it only for what it shares (`Fr.allowed`). The client's answer + keeps the owner's success, error and payload, with the caller's request id; + while it is away the answer says so. Anything + else about a far thread is refused here (law `far_route_here_stays`, + `far_allowed_only_methods`). +- The same view. A mirrored bot is a bot in the read model, so the window, + the web and the phones show it with the same header, tabs, space and + chat, its thread with the same timeline and composer; only its name + carries its machine ("Kit · box", `Label.title` and `Ui.far.title`). Its tabs are what + its machine shares (chat, space, memory, routines; `BU.tabs.of`). A reported-only + choice adopts the full thread when its mirror arrives, keeping an unfinished + remote composer in place until it has been sent (`Far.arrived`). Ordinary + thread rows, project sections and timeline headers use the same machine + marker. On phones paired with both hubs, the paired owner's catalog + replaces mirrored sections/rows once it has loaded (`Hubs.catalog.*`); + its cached mirror stays visible until then. Direct paired-owner Active rows and focused thread/bot headers keep the machine name too; an offline owner is marked away, and an already-marked mirror keeps its original title. When the paired owner differs from + the focused hub, its machine badge remains visible. Hidden Older and Settled + counts also drop duplicates. +- Not mirrored: streamed text while a far turn runs (its messages arrive as + they are posted), file viewers for far project threads, and a bot's browser. + +`test/far_test.bend` (the client's fold, routing, sharing) and +`test/tools/far_e2e.ts` (two hubs: a bot made on one shows on the other, +a message from the other reaches it and its answer comes back, away when +it stops, from disk after a restart). diff --git a/mobile/android/app/src/main/java/dev/backplane/mobile/Bots.kt b/mobile/android/app/src/main/java/dev/backplane/mobile/Bots.kt index 08cc8ec41d..7489967df5 100644 --- a/mobile/android/app/src/main/java/dev/backplane/mobile/Bots.kt +++ b/mobile/android/app/src/main/java/dev/backplane/mobile/Bots.kt @@ -152,7 +152,7 @@ fun LazyListScope.botsSection(m: AppModel, s: Screen) { }, leadingContent = { BotCat(m, b.cat, 40.dp) }, modifier = Modifier.alpha(if (b.mood == "away") 0.5f else 1f) - .clickable { m.act(if (b.remote) "remote" else "bot", b.id) }, + .clickable { m.act(b.act, b.id) }, ) } items(s.rooms, key = { "r:" + it.id }) { r -> @@ -238,10 +238,10 @@ private fun BotHeader(m: AppModel, b: BotView, size: Dp) { horizontalArrangement = Arrangement.spacedBy(12.dp)) { Box(Modifier.alpha(if (b.mood == "away") 0.5f else 1f)) { BotCat(m, b.cat, size) } Column { - Text(b.name, style = MaterialTheme.typography.titleLarge, maxLines = 1, overflow = TextOverflow.Ellipsis) + Text(b.title, style = MaterialTheme.typography.titleLarge, maxLines = 1, overflow = TextOverflow.Ellipsis) Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) { MoodDot(b.mood) - Text(listOf(b.note, b.peer).filter { it.isNotEmpty() }.joinToString(" · "), + Text(listOf(b.note, if (b.title == b.name) b.peer else "").filter { it.isNotEmpty() }.joinToString(" · "), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.outline) } } diff --git a/mobile/android/app/src/main/java/dev/backplane/mobile/Screen.kt b/mobile/android/app/src/main/java/dev/backplane/mobile/Screen.kt index 0c1ec45f19..051334c18f 100644 --- a/mobile/android/app/src/main/java/dev/backplane/mobile/Screen.kt +++ b/mobile/android/app/src/main/java/dev/backplane/mobile/Screen.kt @@ -240,12 +240,13 @@ data class HubRow(val key: String, val name: String, val online: Boolean) data class Found(val name: String, val url: String) -// Bots (src/mobile/bots.bend). A cat in the list: "bot" (or, remote, -// "remote") sends its id; mood and note say how it is; cat names its rig -// ("look:mood"), which the bridge gives once (Core.cats). +// Bots (src/mobile/bots.bend). A cat in the list: act ("bot", or +// "remote" for a bot only reported by a linked machine) sends its id; +// mood and note say how it is; cat names its rig ("look:mood"), which the +// bridge gives once (Core.cats). data class BotRow( val id: String, val name: String, val mood: String, val note: String, val peer: String, val cat: String, - val look: Int, val sel: Boolean, val remote: Boolean, val machine: String, + val look: Int, val sel: Boolean, val remote: Boolean, val machine: String, val act: String, ) // a room in the list ("room" sends its id) @@ -290,6 +291,8 @@ data class BotView( val cat: String, val tabs: List, val space: SpaceModel?, val page: SpacePageModel?, val browser: BotBrowser?, val memory: List, val routines: List, val routine: RoutineForm?, val hooks: List, val settings: BotSettings?, val posts: List, + // the name as shown: "Kit · desk" for a bot on a linked machine + val title: String, ) // hub: the one in focus (its thread is shown, its plots are drawn) @@ -494,6 +497,7 @@ private fun botView(o: JSONObject) = BotView( o.optJSONArray("posts").map { BotPost(it.optString("id"), it.optString("from"), it.optString("text"), it.optString("ago"), it.optBoolean("mine")) }, + o.optString("title", o.optString("name")), ) fun parseScreen(o: JSONObject) = Screen( @@ -519,7 +523,8 @@ fun parseScreen(o: JSONObject) = Screen( o.optJSONArray("found").map { Found(it.optString("name"), it.optString("url")) }, o.optJSONArray("bots").map { BotRow(it.optString("id"), it.optString("name"), it.optString("mood"), it.optString("note"), it.optString("peer"), - it.optString("cat"), it.optInt("look"), it.optBoolean("sel"), it.optBoolean("remote"), it.optString("machine")) + it.optString("cat"), it.optInt("look"), it.optBoolean("sel"), it.optBoolean("remote"), it.optString("machine"), + it.optString("act", if (it.optBoolean("remote")) "remote" else "bot")) }, o.optJSONArray("rooms").map { RoomRow(it.optString("id"), it.optString("name"), it.optInt("members"), it.optBoolean("sel"), it.optString("machine")) diff --git a/mobile/ios/Backplane/AppModel.swift b/mobile/ios/Backplane/AppModel.swift index 1d388187ef..5de5d4a841 100644 --- a/mobile/ios/Backplane/AppModel.swift +++ b/mobile/ios/Backplane/AppModel.swift @@ -406,7 +406,7 @@ final class AppModel { } if let n = botting, let b = s.bots.first(where: { $0.name == n }) { botting = nil - act(b.remote ? "remote" : "bot", b.id) + act(b.act ?? (b.remote ? "remote" : "bot"), b.id) } else if let n = botting, let r = s.rooms.first(where: { $0.name == n }) { botting = nil act("room", r.id) diff --git a/mobile/ios/Backplane/Bots.swift b/mobile/ios/Backplane/Bots.swift index 5688290f09..d7d2951461 100644 --- a/mobile/ios/Backplane/Bots.swift +++ b/mobile/ios/Backplane/Bots.swift @@ -49,7 +49,7 @@ struct BotsSection: View { var body: some View { Section { ForEach(screen.bots) { b in - Button { model.act(b.remote ? "remote" : "bot", b.id) } label: { + Button { model.act(b.act ?? (b.remote ? "remote" : "bot"), b.id) } label: { HStack(spacing: 12) { BotCat(model: model, key: b.cat, size: 36) VStack(alignment: .leading, spacing: 2) { @@ -206,10 +206,10 @@ private struct BotHeader: View { BotCat(model: model, key: bot.cat ?? "", size: size) .saturation(bot.mood == "away" ? 0 : 1) VStack(alignment: .leading, spacing: 2) { - Text(bot.name).font(.title3.weight(.semibold)).lineLimit(1) + Text(bot.title ?? bot.name).font(.title3.weight(.semibold)).lineLimit(1) HStack(spacing: 5) { MoodDot(mood: bot.mood ?? "") - Text([bot.note ?? "", bot.peer ?? ""].filter { !$0.isEmpty }.joined(separator: " · ")).lineLimit(1) + Text([bot.note ?? "", bot.title == nil ? bot.peer ?? "" : ""].filter { !$0.isEmpty }.joined(separator: " · ")).lineLimit(1) } .font(.subheadline) .foregroundStyle(.secondary) diff --git a/mobile/ios/Backplane/Screen.swift b/mobile/ios/Backplane/Screen.swift index e9fc4b128b..34e98d4dce 100644 --- a/mobile/ios/Backplane/Screen.swift +++ b/mobile/ios/Backplane/Screen.swift @@ -424,11 +424,14 @@ struct Found: Decodable, Hashable { let name, url: String } -// Bots (src/mobile/bots.bend). A cat in the list: "bot" (or, remote, -// "remote") sends its id; mood and note say how it is; cat names its rig -// ("look:mood"), which the bridge gives once (AppModel.cats). +// Bots (src/mobile/bots.bend). A cat in the list: act ("bot", or +// "remote" for a bot only reported by a linked machine; older bridges +// leave it out and say remote) sends its id; mood and note say how it is; +// cat names its rig ("look:mood"), which the bridge gives once +// (AppModel.cats). struct BotRow: Decodable, Identifiable { let id, name, mood, note, peer, cat: String + let act: String? let look: Int let sel, remote: Bool let machine: String? @@ -520,6 +523,8 @@ struct BotPost: Decodable, Identifiable { // on a linked machine ("remote") struct BotView: Decodable { let kind, id, name: String + // the name as shown: "Kit · desk" for a bot on a linked machine + let title: String? let mood, note, tab, peer, members, draft, secret, secretFor, hname, cat: String? let look: Int? let tabs: [BotTab]? diff --git a/src/app/bots.bend b/src/app/bots.bend index 941bdb5190..8e2aa90fcf 100644 --- a/src/app/bots.bend +++ b/src/app/bots.bend @@ -32,23 +32,21 @@ def Bv.head(+ty: L.Ty, +c: D.Clip, +label: String, action: String, +on: Bool, +y L.Out{List.append(&2, R.Op, L.Ty.text(ty, 2n, label, c, 16, y, 28, T.Th.faint()), L.Lay.icon(ty, Bool.pick(String, on, "x", "plus"), c, 236, (y + 6 : U32), T.Th.faint())), [L.Lay.hit(228, y, 30, 28, action, "")]} -def Bv.title(+name: String, +remote: Bool, +peer: String) -> String: - Bool.pick(String, Bool.and(remote, Bool.not(String.is_empty(peer))), name ++ " · " ++ peer, name) - def Bv.note.color(+mood: String) -> U32: Bool.pick(U32, String.eq(mood, "wait"), T.Th.warn(), Bool.pick(U32, String.eq(mood, "error"), T.Th.bad(), T.Th.faint())) -# a cat's row: the cat, its name, and its mood -def Bv.row(+ty: L.Ty, +cats: Map<&2, CA.CatF>, +ms: Nat, r: BU.BRow, +c: D.Clip, +y: U32) -> L.Out: +# a cat's row: the cat, its name (with its machine when elsewhere), and +# its mood; choosing it does what BU.act says +def Bv.row(+ty: L.Ty, +cats: Map<&2, CA.CatF>, +ms: Nat, +r: BU.BRow, +c: D.Clip, +y: U32) -> L.Out: match r: - case BU.BRow{+id, +name, _, +look, +mood, +note, +sel, +remote, +peer, _}: + case BU.BRow{+id, _, _, +look, +mood, +note, +sel, _, _, _}: +nw = L.Ty.width(ty, 2n, note) L.Out{List.concat(&2, R.Op, [ Bool.pick(List<&2, R.Op>, sel, [D.Draw.rect(c, 6, (y + 1 : U32), 252, 34, T.Th.raise(), 255), D.Draw.rect(c, 6, (y + 1 : U32), 2, 34, T.Th.accent(), 255)], Nil{}), CA.Cats.draw(cats, look, mood, ms, c, 14, (y + 4 : U32), 28), - L.Ty.text(ty, 0n, L.Ty.fit(ty, 0n, Bv.title(name, remote, peer), L.U32.dim(192, nw)), c, 50, y, 36, Bool.pick(U32, sel, T.Th.text(), T.Th.soft())), + L.Ty.text(ty, 0n, L.Ty.fit(ty, 0n, BU.BU.row.title(r), L.U32.dim(192, nw)), c, 50, y, 36, Bool.pick(U32, sel, T.Th.text(), T.Th.soft())), L.Ty.text(ty, 2n, note, c, (252 - nw : U32), y, 36, Bv.note.color(mood))]), - [L.Lay.hit(0, y, 264, 36, Bool.pick(String, remote, "remote", "bot"), id)]} + [L.Lay.hit(0, y, 264, 36, BU.BU.act(r), id)]} def Bv.rows(rs: List<&2, BU.BRow>, +ty: L.Ty, +cats: Map<&2, CA.CatF>, +ms: Nat, +c: D.Clip, +y: U32) -> L.Col: match rs: @@ -147,10 +145,10 @@ def Bv.side(+ty: L.Ty, +cats: Map<&2, CA.CatF>, +n: S.Nui, +c: D.Clip, +y: U32) Bv.side.of(Bv.sd(ty, cats, n), c, y) # the selected cat is also drawn large at the top of its view -def Bv.warm.sel(sel: Bool, c: Map<&2, CA.CatF>, +look: Nat, +mood: String, +remote: Bool, +clock: Nat) -> Map<&2, CA.CatF>: +def Bv.warm.sel(sel: Bool, c: Map<&2, CA.CatF>, +look: Nat, +mood: String, +clock: Nat) -> Map<&2, CA.CatF>: match sel: case True{}: - CA.Cats.warm(c, look, mood, Bool.pick(U32, remote, 36, 64), clock) + CA.Cats.warm(c, look, mood, 64, clock) case False{}: c @@ -165,9 +163,9 @@ def Bv.warm.go(rs: List<&2, BU.BRow>, c: Map<&2, CA.CatF>, +clock: Nat) -> Map<& match rs: case Nil{}: c - case BU.BRow{_, _, _, +look, +mood, _, sel, +remote, _, _} <> t: + case BU.BRow{_, _, _, +look, +mood, _, sel, _, _, _} <> t: +at = Bv.warm.at(mood, clock) - Bv.warm.go(t, Bv.warm.sel(sel, CA.Cats.warm(c, look, mood, 28, at), look, mood, remote, at), clock) + Bv.warm.go(t, Bv.warm.sel(sel, CA.Cats.warm(c, look, mood, 28, at), look, mood, at), clock) def Bv.warm(+u: U.Ui, c: Map<&2, CA.CatF>, +clock: Nat) -> Map<&2, CA.CatF>: Bv.warm.go(BU.BU.rows(u), c, clock) @@ -247,11 +245,20 @@ def Bv.nl(+ls: List<&2, String>) -> U32: # Memory # ------ -def Bv.mem(+ty: L.Ty, m: BT.Mem, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: +# o when on, else nothing +def Bv.when(on: Bool, o: L.Out) -> L.Out: + match on: + case True{}: + o + case False{}: + L.Out.none() + +# a memory; Forget only for a bot here (BU.owns) +def Bv.mem(+ty: L.Ty, m: BT.Mem, +own: Bool, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: match m: case BT.Mem{_, +key, +kind, +text, +tags, _, _}: +kw = L.Ty.width(ty, 2n, kind) - +fw = L.Lay.button.w(ty, "Forget") + +fw = {Bool.pick(U32, own, L.Lay.button.w(ty, "Forget"), 0) : U32} +ls = L.Ty.wrap(ty, 3n, text, (w - 24 : U32)) +th = {Bool.pick(U32, String.is_empty(tags), 0, 20) : U32} +h = {(48 + Bv.nl(ls) * 22 + th : U32) : U32} @@ -259,24 +266,24 @@ def Bv.mem(+ty: L.Ty, m: BT.Mem, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col L.Out.ops(L.Lay.box(c, x, y, w, h, 0, T.Th.panel(), T.Th.line())), L.Out.ops(L.Ty.text(ty, 0n, L.Ty.fit(ty, 0n, key, L.U32.dim(w, (kw + fw + 48 : U32))), c, (x + 12 : U32), (y + 6 : U32), 28, T.Th.text())), L.Out.ops(L.Ty.text(ty, 2n, kind, c, (x + w - fw - 20 - kw : U32), (y + 6 : U32), 28, T.Th.accent())), - F.Fm.button(ty, c, (x + w - fw - 8 : U32), (y + 6 : U32), "Forget", T.Th.soft(), "mem-forget", key), + Bv.when(own, F.Fm.button(ty, c, (x + w - fw - 8 : U32), (y + 6 : U32), "Forget", T.Th.soft(), "mem-forget", key)), L.Out.ops(Bv.lines(ls, ty, c, (x + 12 : U32), (y + 38 : U32), T.Th.soft())), Bv.faint(ty, c, tags, (x + 12 : U32), (w - 24 : U32), (y + 40 + Bv.nl(ls) * 22 : U32))]), (y + h + 8 : U32)} -def Bv.mems(ms: List<&2, BT.Mem>, +ty: L.Ty, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: +def Bv.mems(ms: List<&2, BT.Mem>, +ty: L.Ty, +own: Bool, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: match ms: case Nil{}: L.Col{L.Out.none(), y} case m <> t: - L.Lay.col.then(Bv.mem(ty, m, c, x, w, y), y2 => Bv.mems(t, ty, c, x, w, y2)) + L.Lay.col.then(Bv.mem(ty, m, own, c, x, w, y), y2 => Bv.mems(t, ty, own, c, x, w, y2)) def Bv.memory(+ty: L.Ty, +u: U.Ui, +bot: String, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: +ms = BU.BU.mems(u, bot) L.Lay.col.cat(L.Out.cat(Bv.h2(ty, c, "Memory", x, y), Bv.faint(ty, c, Bool.pick(String, List.is_empty(&2, BT.Mem, ms), "Nothing remembered yet. It keeps what it learns about you, your preferences, facts and how-tos here.", Nat.show(List.length(&2, BT.Mem, ms)) ++ " items. The ones about you and your preferences go into every turn."), x, w, (y + 30 : U32))), - Bv.mems(ms, ty, c, x, w, (y + 60 : U32))) + Bv.mems(ms, ty, BU.BU.owns(bot), c, x, w, (y + 60 : U32))) # Routines # -------- @@ -303,12 +310,33 @@ def Bv.routine(+ty: L.Ty, r: BT.Routine, +now: Nat, +c: D.Clip, +x: U32, +w: U32 L.Out.ops(L.Ty.text(ty, 2n, L.Ty.fit(ty, 2n, prompt, (w - 24 : U32)), c, (x + 12 : U32), (y + 62 : U32), 20, T.Th.soft()))]), (y + 98 : U32)} -def Bv.routines(rs: List<&2, BT.Routine>, +ty: L.Ty, +now: Nat, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: +# a routine of a bot elsewhere: its hub runs it on asking, nothing more +def Bv.routine.far(+ty: L.Ty, r: BT.Routine, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: + match r: + case BT.Routine{+id, _, +name, +cron, +prompt, +on, _, _, _}: + +b3 = L.Lay.button.w(ty, "Run now") + +bx = {(x + w - 12 - b3 : U32) : U32} + L.Col{L.Out.all([ + L.Out.ops(L.Lay.box(c, x, y, w, 90, 0, T.Th.panel(), T.Th.line())), + L.Out.ops(L.Ty.text(ty, 0n, L.Ty.fit(ty, 0n, name, L.U32.dim((bx - x : U32), 24)), c, (x + 12 : U32), (y + 8 : U32), 28, Bool.pick(U32, on, T.Th.text(), T.Th.dim()))), + F.Fm.button(ty, c, bx, (y + 8 : U32), "Run now", T.Th.soft(), "routine-run", id), + Bv.faint(ty, c, CR.Cron.describe(cron) ++ Bool.pick(String, on, "", " · off"), (x + 12 : U32), (w - 24 : U32), (y + 40 : U32)), + L.Out.ops(L.Ty.text(ty, 2n, L.Ty.fit(ty, 2n, prompt, (w - 24 : U32)), c, (x + 12 : U32), (y + 62 : U32), 20, T.Th.soft()))]), + (y + 98 : U32)} + +def Bv.routine.k(own: Bool, +ty: L.Ty, r: BT.Routine, +now: Nat, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: + match own: + case True{}: + Bv.routine(ty, r, now, c, x, w, y) + case False{}: + Bv.routine.far(ty, r, c, x, w, y) + +def Bv.routines(rs: List<&2, BT.Routine>, +ty: L.Ty, +own: Bool, +now: Nat, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: match rs: case Nil{}: L.Col{L.Out.none(), y} case r <> t: - L.Lay.col.then(Bv.routine(ty, r, now, c, x, w, y), y2 => Bv.routines(t, ty, now, c, x, w, y2)) + L.Lay.col.then(Bv.routine.k(own, ty, r, now, c, x, w, y), y2 => Bv.routines(t, ty, own, now, c, x, w, y2)) def Bv.oform(+ty: L.Ty, +f: F.FS, +editing: Bool, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: L.Col{L.Out.all([ @@ -320,13 +348,21 @@ def Bv.oform(+ty: L.Ty, +f: F.FS, +editing: Bool, +c: D.Clip, +x: U32, +w: U32, F.Fm.button(ty, c, x, (y + 186 : U32), Bool.pick(String, editing, "Save", "Add routine"), T.Th.accent(), "routine-save", "")]), (y + 230 : U32)} +# the new routine form, for a bot here +def Bv.oform.if(own: Bool, +ty: L.Ty, +f: F.FS, +editing: Bool, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: + match own: + case True{}: + Bv.oform(ty, f, editing, c, x, w, y) + case False{}: + L.Col{L.Out.none(), y} + def Bv.rtab(+ty: L.Ty, +f: F.FS, +bot: String, +c: D.Clip, +x: U32, +w: U32, +y: U32) -> L.Col: +rs = BU.BU.routines(F.FS.ui(f), bot) L.Lay.col.cat(L.Out.cat(Bv.h2(ty, c, "Routines", x, y), Bv.faint(ty, c, Bool.pick(String, List.is_empty(&2, BT.Routine, rs), "None yet. A routine gives it a prompt on a schedule, in this hub's time zone.", "Each runs once when due, even after the hub was off."), x, w, (y + 30 : U32))), - L.Lay.col.then(Bv.routines(rs, ty, U.Side.now(F.FS.ui(f)), c, x, w, (y + 60 : U32)), y2 => - Bv.oform(ty, f, BU.BU.open(F.FS.ui(f), "@onew"), c, x, w, (y2 + 12 : U32)))) + L.Lay.col.then(Bv.routines(rs, ty, BU.BU.owns(bot), U.Side.now(F.FS.ui(f)), c, x, w, (y + 60 : U32)), y2 => + Bv.oform.if(BU.BU.owns(bot), ty, f, BU.BU.open(F.FS.ui(f), "@onew"), c, x, w, (y2 + 12 : U32)))) # Webhooks # -------- @@ -546,6 +582,7 @@ def Bv.busy(s: M.Status) -> Bool: case _: False{} +# the stop button, at the header's right (x1: where it ends) def Bv.stop(on: Bool, +ty: L.Ty, +x1: U32) -> L.Out: match on: case True{}: @@ -553,24 +590,28 @@ def Bv.stop(on: Bool, +ty: L.Ty, +x1: U32) -> L.Out: case False{}: L.Out.none() -# the bot's cat (64 px), its name and mood, its tabs, and its own viewer -# (board, schematic, 3D, browser, diff) opened or shut beside them -def Bv.header(+ty: L.Ty, +cats: Map<&2, CA.CatF>, +u: U.Ui, +ms: Nat, b: BT.Bot, +tab: String, +x0: U32, +x1: U32) -> L.Out: - match b: - case BT.Bot{+id, +name, +th, +look, pe, cr, de}: - +st = U.Ui.state(u) - +mood = BU.Mood.bot(st, U.Side.now(u), BT.Bot{id, name, th, look, pe, cr, de}) - +t = U.Ui.thread(st, th) - +nw = L.Ty.width(ty, 1n, name) +# where the header's right side ends before the Viewer chip (when shown) +def Bv.right(+viewer: Bool, +ty: L.Ty, +x1: U32) -> U32: + Bool.pick(U32, viewer, (x1 - 8 - L.Lay.button.w(ty, "Panel") : U32), x1) + +# a bot's header (BU.head, the same for a bot here, one mirrored from a +# linked machine and one only reported by one): its cat (64 px), its name +# with where it lives, its mood (and sub), what goes at its right (Stop), +# its own viewer (board, schematic, 3D, browser, diff) when it has one, +# and its tabs +def Bv.header(+ty: L.Ty, +cats: Map<&2, CA.CatF>, +u: U.Ui, +ms: Nat, hd: BU.Head, +sub: String, right: L.Out, +x0: U32, +x1: U32) -> L.Out: + match hd: + case BU.Head{+title, +look, +mood, +note, ts, +tab, +viewer, _}: + +nw = L.Ty.width(ty, 1n, title) L.Out.all([ L.Out{[D.Draw.rect(D.Clip.all(), x0, 0, (x1 - x0 : U32), Bv.hy(), T.Th.bg(), 255), D.Draw.rect(D.Clip.all(), x0, (Bv.hy() - 1 : U32), (x1 - x0 : U32), 1, T.Th.line(), 255)], [L.Lay.hit(x0, 0, (x1 - x0 : U32), Bv.hy(), "", "")]}, L.Out.ops(CA.Cats.draw(cats, look, mood, ms, D.Clip.all(), (x0 + 20 : U32), 12, 64)), - L.Out.ops(L.Ty.text(ty, 1n, name, D.Clip.all(), (x0 + 100 : U32), 10, 30, T.Th.text())), - L.Out.ops(L.Ty.text(ty, 2n, BU.Mood.note(mood) ++ " · " ++ M.Thread.provider(t), D.Clip.all(), (x0 + 112 + nw : U32), 10, 30, Bv.note.color(mood))), - Bv.stop(Bv.busy(U.Side.status(st, U.Ui.thread(st, th))), ty, (x1 - 8 - L.Lay.button.w(ty, "Panel") : U32)), - F.Fm.chip(ty, D.Clip.all(), (x1 - 16 - L.Lay.button.w(ty, "Panel") : U32), 12, "Panel", U.Ui.panel(u), "panel", ""), - Bv.tabs(BU.BU.tabs(), ty, tab, (x0 + 100 : U32), 48)]) + L.Out.ops(L.Ty.text(ty, 1n, title, D.Clip.all(), (x0 + 100 : U32), 10, 30, T.Th.text())), + L.Out.ops(L.Ty.text(ty, 2n, note ++ sub, D.Clip.all(), (x0 + 112 + nw : U32), 10, 30, Bv.note.color(mood))), + right, + Bv.when(viewer, F.Fm.chip(ty, D.Clip.all(), (x1 - 16 - L.Lay.button.w(ty, "Panel") : U32), 12, "Panel", U.Ui.panel(u), "panel", "")), + Bv.tabs(ts, ty, tab, (x0 + 100 : U32), 48)]) def Bv.tab.n(+t: String) -> Nat: S.Nui.pos(BU.BU.tabs(), t, 0n) @@ -601,9 +642,18 @@ def Bv.bot.k(k: Nat, +ty: L.Ty, +memo: L.TLMemo, +n: S.Nui, +b: BT.Bot, +x0: U32 Bv.pane(Bv.pane.k(j, ty, F.Fm.fs(n), b, D.Clip{x0, Bv.hy(), x1, S.Nui.height(n)}, Bv.pane.x(x0, x1), Bv.pane.w(x0, x1), (top - off : U32)), x0, x1, Bv.hy(), off, S.Nui.height(n)) +# a bot, here or mirrored from a linked machine: its header, then the tab +# BU.head picks (the tabs it has) +def Bv.bot.h(+ty: L.Ty, +memo: L.TLMemo, +cats: Map<&2, CA.CatF>, +n: S.Nui, +b: BT.Bot, +hd: BU.Head, +x0: U32, +x1: U32) -> L.Out: + +u = S.Nui.ui(n) + +st = U.Ui.state(u) + +th = BT.Bot.thread(b) + L.Out.cat(Bv.bot.k(Bv.tab.n(BU.Head.tab(hd)), ty, memo, n, b, x0, x1), + Bv.header(ty, cats, u, S.Nui.ms_of(n), hd, " · " ++ M.Thread.provider(U.Ui.thread(st, th)), + Bv.stop(Bv.busy(U.Side.status(st, U.Ui.thread(st, th))), ty, Bv.right(BU.Head.viewer(hd), ty, x1)), x0, x1)) + def Bv.bot(+ty: L.Ty, +memo: L.TLMemo, +cats: Map<&2, CA.CatF>, +n: S.Nui, +b: BT.Bot, +x0: U32, +x1: U32) -> L.Out: - +tab = BU.BU.tab(S.Nui.ui(n)) - L.Out.cat(Bv.bot.k(Bv.tab.n(tab), ty, memo, n, b, x0, x1), Bv.header(ty, cats, S.Nui.ui(n), S.Nui.ms_of(n), b, tab, x0, x1)) + Bv.bot.h(ty, memo, cats, n, b, BU.BU.head.bot(S.Nui.ui(n), b), x0, x1) # Rooms and bots elsewhere # ------------------------ @@ -613,12 +663,16 @@ def Bv.bot(+ty: L.Ty, +memo: L.TLMemo, +cats: Map<&2, CA.CatF>, +n: S.Nui, +b: B type PostL is Data: PostL{id: String, from: String, when: String, lines: List<&2, String>} -# Wrapped posts kept from frame to frame (main.bend's Kept): a post's -# text and its lines at a width, under ":". Layout takes the -# lines kept only for the same text (a post's id alone could come back -# with another; a missing glyph's fetch starts them over, Shell.paint.again) +# Wrapped posts kept from frame to frame (main.bend's Kept). The outer +# map holds eight recently used conversation/width groups and their order; +# each group's map holds its posts under ":". A post is reused +# only for the same text. A missing glyph's fetch starts every group over +# (Shell.paint.again). The nested map sits behind a List to keep native +# state records from flattening it into too many fields. type PW is Data: PW{ok: Bool, text: String, lines: List<&2, String>} + PWGroup{posts: List<&2, Map<&2, PW>>} + PWKeys{keys: List<&2, String>} def Bv.wraps.none() -> Map<&2, PW>: Map.new(&2, PW) @@ -641,7 +695,7 @@ def Bv.wrap.k(p: PW, +ty: L.Ty, +text: String, +w: U32) -> List<&2, String>: match p: case PW{True{}, +t, ls}: Bv.wrap.same(Q.Eq.str(t, text), ls, ty, text, w) - case PW{False{}, _, _}: + case _: L.Ty.wrap(ty, 3n, text, w) # a post's lines: the kept ones when they wrapped the same text, else @@ -649,6 +703,21 @@ def Bv.wrap.k(p: PW, +ty: L.Ty, +text: String, +w: U32) -> List<&2, String>: def Bv.wrap(+ws: Map<&2, PW>, +ty: L.Ty, +id: String, +text: String, +w: U32) -> List<&2, String>: Bv.wrap.k(Bv.wrap.got(Map.get(PW, PW{False{}, "", Nil{}}, ws, Bv.wrap.key(id, w))), ty, text, w) +# Room ids include a remote DM's stable conversation key. Width is part +# of the group key, so returning after a resize can reuse either layout. +def Bv.wraps.key(+room: String, +w: U32) -> String: + "c:" ++ Bv.wrap.key(room, w) + +def Bv.wraps.group.k(p: PW) -> Map<&2, PW>: + match p: + case PWGroup{ws <> _}: + ws + case _: + Bv.wraps.none() + +def Bv.wraps.group(+ws: Map<&2, PW>, +room: String, +w: U32) -> Map<&2, PW>: + Bv.wraps.group.k(Bv.wrap.got(Map.get(PW, PW{False{}, "", Nil{}}, ws, Bv.wraps.key(room, w)))) + def Bv.posts.l(ps: List<&2, BT.Post>, +ws: Map<&2, PW>, +ty: L.Ty, +u: U.Ui, +now: Nat, +w: U32) -> List<&2, PostL>: match ps: case Nil{}: @@ -736,32 +805,21 @@ def Bv.room(+ty: L.Ty, +ws: Map<&2, PW>, +n: S.Nui, r: BT.Room, +x0: U32, +x1: U +h = S.Nui.height(n) +dw = L.Lay.button.w(ty, "Delete room") L.Out.all([ - Bv.chat(ty, ws, BU.BU.posts(u, id), u, "Nothing said here yet. Every bot in the room hears what is posted.", x0, x1, 52, (h - 60 : U32)), + Bv.chat(ty, Bv.wraps.group(ws, id, Bv.pane.w(x0, x1)), BU.BU.posts(u, id), u, "Nothing said here yet. Every bot in the room hears what is posted.", x0, x1, 52, (h - 60 : U32)), Bv.say(ty, F.Fm.fs(n), "rpost", "room-post", "Write to the room", x0, x1, h), Bv.top(ty, "# " ++ name, BU.BU.members(u, BT.Room{id, name, ms, cr, de}), x0, x1, F.Fm.button(ty, D.Clip.all(), (x1 - 16 - dw : U32), 12, "Delete room", T.Th.soft(), "room-delete", id))]) -def Bv.far.find(rs: List<&2, BU.BRow>, +id: String) -> Maybe<&2, BU.BRow>: - match rs: - case Nil{}: - None{} - case BU.BRow{+i, +na, +th, +lk, +mo, +no, +se, +re, +pe, +l} <> t: - Bool.pick(Maybe<&2, BU.BRow>, String.eq(i, id), Some{BU.BRow{i, na, th, lk, mo, no, se, re, pe, l}}, Bv.far.find(t, id)) - -def Bv.far.cat(m: Maybe<&2, BU.BRow>, +cats: Map<&2, CA.CatF>, +ms: Nat, +x0: U32) -> L.Out: - match m: - case Some{BU.BRow{_, _, _, +look, +mood, +note, _, _, _, _}}: - L.Out.ops(List.append(&2, R.Op, CA.Cats.draw(cats, look, mood, ms, D.Clip.all(), (x0 + 16 : U32), 8, 36), [])) - case None{}: - L.Out.none() - +# a bot only reported by a linked machine: the bot's view (BU.head: its +# cat, "name · machine", its mood, Chat alone) over the person's +# conversation with it def Bv.remote(+ty: L.Ty, +ws: Map<&2, PW>, +cats: Map<&2, CA.CatF>, +n: S.Nui, +id: String, +x0: U32, +x1: U32) -> L.Out: +u = S.Nui.ui(n) +h = S.Nui.height(n) L.Out.all([ - Bv.chat(ty, ws, BU.BU.remote.posts(u, id), u, "Write to " ++ id ++ ". It answers from its own machine.", x0, x1, 52, (h - 60 : U32)), + Bv.chat(ty, Bv.wraps.group(ws, BT.Room.dm("you", id), Bv.pane.w(x0, x1)), BU.BU.remote.posts(u, id), u, "Write to " ++ id ++ ". It answers from its own machine.", x0, x1, Bv.hy(), (h - 60 : U32)), Bv.say(ty, F.Fm.fs(n), "rtext", "remote-send", "Write to " ++ id, x0, x1, h), - Bv.top(ty, " " ++ id, "on a linked machine", x0, x1, Bv.far.cat(Bv.far.find(BU.BU.remote(u), id), cats, S.Nui.ms_of(n), x0))]) + Bv.header(ty, cats, u, S.Nui.ms_of(n), BU.BU.head(u, BU.VRemote{id}), "", L.Out.none(), x0, x1)]) # The main area and the frame # --------------------------- @@ -803,9 +861,9 @@ def Bv.frame.w(+ty: L.Ty, +memo: L.TLMemo, +cats: Map<&2, CA.CatF>, +ws: Map<&2, def Bv.frame(+ty: L.Ty, +memo: L.TLMemo, +cats: Map<&2, CA.CatF>, +n: S.Nui) -> L.Out: Bv.frame.w(ty, memo, cats, Bv.wraps.none(), n) -# the posts on show wrapped for this frame: those of the room or the bot -# elsewhere shown, at its width, kept from ws where their text is the same -# (only what is shown is kept) +# The posts on show wrapped for this frame, reusing the selected group's +# unchanged texts. Rebuilding this one group removes posts no longer in +# the conversation's bounded window; other groups stay until evicted. def Bv.wraps.put(ps: List<&2, BT.Post>, +ws: Map<&2, PW>, +ty: L.Ty, +w: U32, acc: Map<&2, PW>) -> Map<&2, PW>: match ps: case Nil{}: @@ -813,19 +871,62 @@ def Bv.wraps.put(ps: List<&2, BT.Post>, +ws: Map<&2, PW>, +ty: L.Ty, +w: U32, ac case BT.Post{+id, _, _, +text, _, _} <> t: Bv.wraps.put(t, ws, ty, w, Map.set(&2, PW, acc, Bv.wrap.key(id, w), PW{True{}, text, Bv.wrap(ws, ty, id, text, w)})) +def Bv.wraps.max() -> Nat: + 8n + +def Bv.wraps.keys.k(p: PW) -> List<&2, String>: + match p: + case PWKeys{ks}: + ks + case _: + Nil{} + +def Bv.wraps.keys(+ws: Map<&2, PW>) -> List<&2, String>: + Bv.wraps.keys.k(Bv.wrap.got(Map.get(PW, PWKeys{Nil{}}, ws, "@wraps"))) + +def Bv.wraps.drop.k(same: Bool, k: String, rest: List<&2, String>) -> List<&2, String>: + match same: + case True{}: + rest + case False{}: + k <> rest + +def Bv.wraps.drop(ks: List<&2, String>, +key: String) -> List<&2, String>: + match ks: + case Nil{}: + Nil{} + case +k <> t: + Bv.wraps.drop.k(String.eq(k, key), k, Bv.wraps.drop(t, key)) + +def Bv.wraps.evict(ks: List<&2, String>, ws: Map<&2, PW>) -> Map<&2, PW>: + match ks: + case Nil{}: + ws + case k <> t: + Bv.wraps.evict(t, Map.del(&2, PW, ws, k)) + +def Bv.wraps.keep(+ws: Map<&2, PW>, +key: String, posts: Map<&2, PW>) -> Map<&2, PW>: + +keys = {key <> Bv.wraps.drop(Bv.wraps.keys(ws), key) : List<&2, String>} + Map.set(&2, PW, Map.set(&2, PW, + Bv.wraps.evict(List.drop(&2, String, keys, Bv.wraps.max()), ws), key, PWGroup{[posts]}), + "@wraps", PWKeys{List.take(&2, String, keys, Bv.wraps.max())}) + +def Bv.wraps.conversation(+ps: List<&2, BT.Post>, +room: String, +ws: Map<&2, PW>, +ty: L.Ty, +w: U32) -> Map<&2, PW>: + Bv.wraps.keep(ws, Bv.wraps.key(room, w), Bv.wraps.put(ps, Bv.wraps.group(ws, room, w), ty, w, Bv.wraps.none())) + def Bv.wraps.of(v: BU.View, +u: U.Ui, +ws: Map<&2, PW>, +ty: L.Ty, +w: U32) -> Map<&2, PW>: match v: case BU.VRoom{BT.Room{+id, _, _, _, _}}: - Bv.wraps.put(BU.BU.posts(u, id), ws, ty, w, Bv.wraps.none()) + Bv.wraps.conversation(BU.BU.posts(u, id), id, ws, ty, w) case BU.VRemote{+id}: - Bv.wraps.put(BU.BU.remote.posts(u, id), ws, ty, w, Bv.wraps.none()) + Bv.wraps.conversation(BU.BU.remote.posts(u, id), BT.Room.dm("you", id), ws, ty, w) case _: - Bv.wraps.none() + ws def Bv.wraps.k(settings: Bool, +ty: L.Ty, +n: S.Nui, +ws: Map<&2, PW>) -> Map<&2, PW>: match settings: case True{}: - Bv.wraps.none() + ws case False{}: Bv.wraps.of(BU.BU.view(S.Nui.ui(n)), S.Nui.ui(n), ws, ty, Bv.pane.w(264, L.Lay.x1(n))) diff --git a/src/app/effects/link.c b/src/app/effects/link.c new file mode 100644 index 0000000000..35d1c2aa74 --- /dev/null +++ b/src/app/effects/link.c @@ -0,0 +1,165 @@ +// Bounded hostname resolution and connect, and byte-exact handshake reads. +// DNS runs on a detached worker: the Bend event loop never waits in libc. +#include +#include +#include +#include +#include +#include + +#ifndef MSG_NOSIGNAL +#define MSG_NOSIGNAL 0 +#endif + +#ifdef CID_LINK_CONNECT + +typedef struct { + char* host; + int fd; +} LinkResolve; + +typedef struct { + int code; + struct sockaddr_in addr; +} LinkAddress; + +typedef struct { + u64 deadline; + int phase; + uint16_t port; +} LinkDial; + +static void* link_resolve(void* arg) { + LinkResolve* job = arg; + struct addrinfo hint = {0}, *addrs = NULL; + hint.ai_family = AF_INET; + hint.ai_socktype = SOCK_STREAM; + LinkAddress answer = {0}; + int code = getaddrinfo(job->host, NULL, &hint, &addrs); + if (code || !addrs) { + answer.code = EHOSTUNREACH; + } else { + memcpy(&answer.addr, addrs->ai_addr, sizeof(answer.addr)); + } + if (addrs) freeaddrinfo(addrs); + // The window may already have timed out and closed its endpoint. + send(job->fd, &answer, sizeof(answer), MSG_NOSIGNAL); + close(job->fd); + free(job->host); + free(job); + return NULL; +} + +static Term link_connect_end(Env e, IoWork* w, int code) { + int fd = (int)w->hand; + free(w->data); + if (code) { + close(fd); + return io_fail(e, code, NULL); + } + return io_done(e, io_hand(fd)); +} + +static Term link_connect_more(Env e, IoWork* w) { + LinkDial* dial = (LinkDial*)w->data; + int fd = (int)w->hand; + if (io_tick() >= dial->deadline) return link_connect_end(e, w, ETIMEDOUT); + if (dial->phase == 0) { + LinkAddress answer; + ssize_t n = recv(fd, &answer, sizeof(answer), 0); + if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { + return io_wait_on(w, fd, POLLIN, dial->deadline, link_connect_more); + } + if (n != sizeof(answer)) return link_connect_end(e, w, EHOSTUNREACH); + if (answer.code) return link_connect_end(e, w, answer.code); + answer.addr.sin_port = htons(dial->port); + close(fd); + fd = socket(AF_INET, SOCK_STREAM, 0); + w->hand = fd; + if (fd < 0) return link_connect_end(e, w, errno); + if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0 || fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) { + return link_connect_end(e, w, errno); + } + int code = connect(fd, (struct sockaddr*)&answer.addr, sizeof(answer.addr)) < 0 ? errno : 0; + if (code != EINPROGRESS) return link_connect_end(e, w, code); + dial->phase = 1; + return io_wait_on(w, fd, POLLOUT, dial->deadline, link_connect_more); + } + int code = 0; + socklen_t size = sizeof(code); + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &code, &size) < 0) code = errno; + return link_connect_end(e, w, code); +} + +Term link_connect_run(Env e, Term* f, IoWork* w) { + u64 len = 0; + char* host = io_cstr(e, f[0], &len); + if (io_nul(host, len)) { + free(host); + return io_fail(e, EINVAL, NULL); + } + int pair[2]; + if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) { + free(host); + return io_fail(e, errno, NULL); + } + if (fcntl(pair[0], F_SETFD, FD_CLOEXEC) < 0 || fcntl(pair[1], F_SETFD, FD_CLOEXEC) < 0 || fcntl(pair[0], F_SETFL, fcntl(pair[0], F_GETFL) | O_NONBLOCK) < 0) { + int code = errno; + close(pair[0]); close(pair[1]); free(host); + return io_fail(e, code, NULL); + } + LinkResolve* job = io_mem(malloc(sizeof(*job))); + job->host = host; + job->fd = pair[1]; + pthread_t worker; + int code = pthread_create(&worker, NULL, link_resolve, job); + if (code) { + close(pair[0]); close(pair[1]); free(host); free(job); + return io_fail(e, code, NULL); + } + pthread_detach(worker); + LinkDial* dial = io_mem(malloc(sizeof(*dial))); + dial->deadline = io_tick() + (u64)f[2] * 1000000ull; + dial->phase = 0; + dial->port = (uint16_t)f[1]; + w->data = (char*)dial; + w->hand = pair[0]; + return io_wait_on(w, pair[0], POLLIN, dial->deadline, link_connect_more); +} + +static void __attribute__((constructor)) link_connect_use(void) { + io_eff(CID_LINK_CONNECT, link_connect_run, 0); +} +#endif + +#ifdef CID_LINK_POLL +static Term link_poll_end(Env e, IoWork* w, Term result) { + free(w->data); + return io_tup(e, io_hand(w->hand), result); +} + +static Term link_poll_more(Env e, IoWork* w) { + int fd = (int)w->hand; + u64 deadline = io_wait_time(w); + ssize_t n = recv(fd, w->data, (size_t)w->made, 0); + if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { + return io_tick() < deadline ? io_wait_on(w, fd, POLLIN, deadline, link_poll_more) + : link_poll_end(e, w, io_done(e, term_pak(CID_NONE, 0))); + } + if (n < 0) return link_poll_end(e, w, io_fail(e, errno, NULL)); + Term bytes = term_pak(CID_NIL, 0); + for (ssize_t i = n; i > 0; i--) bytes = io_node(e, CID_CON, (Term)(uint8_t)w->data[i - 1], bytes); + return link_poll_end(e, w, io_done(e, io_box(e, CID_SOME, bytes))); +} + +Term link_poll_run(Env e, Term* f, IoWork* w) { + w->hand = (intptr_t)io_hand_v(f[0]); + w->made = 65536; + w->data = io_mem(malloc((size_t)w->made)); + return io_wait_on(w, (int)w->hand, POLLIN, io_tick() + (u64)f[1] * 1000000ull, link_poll_more); +} + +static void __attribute__((constructor)) link_poll_use(void) { + io_eff(CID_LINK_POLL, link_poll_run, 0); +} +#endif diff --git a/src/app/effects/link.js b/src/app/effects/link.js new file mode 100644 index 0000000000..3c7ef904ed --- /dev/null +++ b/src/app/effects/link.js @@ -0,0 +1,66 @@ +// Native links are shipped as C. The JS twin uses the same deadline and +// preserves every byte when a response head shares a read with a CBOR frame. +function link_connect(host, port, ms, k) { + const sys = io_sys(); + const at = performance.now() + Number(ms); + let address = host; + // Bend's JS runner polls synchronously; a Node DNS callback cannot wake + // its parked work. Use the platform resolver as a bounded subprocess in + // this quick-check twin. The native effect resolves off the event loop. + if (!/^\d+\.\d+\.\d+\.\d+$/.test(host)) { + try { + const exec = require("node:child_process").execFileSync; + const text = sys.mac + ? exec("dscacheutil", ["-q", "host", "-a", "name", host], { timeout: Number(ms), encoding: "utf8" }) + : exec("getent", ["ahostsv4", host], { timeout: Number(ms), encoding: "utf8" }); + const match = text.match(/\b\d+\.\d+\.\d+\.\d+\b/); + if (!match) return io_fail(113); + address = match[0]; + } catch (_) { return io_fail(113); } + } + if (performance.now() >= at) return io_fail(110); + const addr = io_addr(address, Number(port)); + if (addr === null) return io_fail(22); + const fd = sys.socket(2, 1, 0); + if (fd < 0) return io_fail(sys.errno()); + const end = (code) => { + if (code) { sys.close(fd); return io_fail(code); } + return io_done(fd); + }; + const go = () => { + if (performance.now() >= at) return end(110); + const error = new Int32Array([0]); + const len = new Uint32Array([4]); + const result = sys.getsockopt(fd, sys.mac ? 0xffff : 1, sys.mac ? 0x1007 : 4, sys.ptr(error), sys.ptr(len)); + return end(result < 0 ? sys.errno() : error[0]); + }; + const flags = sys.fcntl(fd, 4, sys.fcntl(fd, 3, 0) | (sys.mac ? 4 : 0x800)); + if (flags < 0) return end(sys.errno()); + const result = sys.connect(fd, sys.ptr(addr), 16); + if (result >= 0) return end(0); + const code = sys.errno(); + if (code !== (sys.mac ? 36 : 115)) return end(code); + io_park_on(fd, true, k, go, at); + return undefined; +} + +function link_poll(socket, ms, k) { + const sys = io_sys(); + const buf = new Uint8Array(65536); + const at = performance.now() + Number(ms); + const list = (n) => { + let xs = { $: "Nil" }; + for (let i = n; i > 0; i--) xs = { $: "Con", head: buf[i - 1], tail: xs }; + return xs; + }; + const go = () => { + const n = Number(sys.recv(socket, sys.ptr(buf), buf.length, 0)); + if (n >= 0) return io_tup(socket, io_done({ $: "Some", value: list(n) })); + const code = sys.errno(); + if (code !== (sys.mac ? 35 : 11)) return io_tup(socket, io_fail(code)); + if (performance.now() >= at) return io_tup(socket, io_done({ $: "None" })); + io_park_on(socket, false, k, go, at); + return undefined; + }; + return go(); +} diff --git a/src/app/layout.bend b/src/app/layout.bend index bb37aabc20..242b323021 100644 --- a/src/app/layout.bend +++ b/src/app/layout.bend @@ -288,7 +288,7 @@ def Lay.row.on(+ty: Ty, +t: M.Thread, +st: M.State, +sel: String, +now: Nat, +sn match t: case M.Thread{+id, +p, title, _, +a, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}: +b = SB.Subs.badge(SB.Subs.count(st, id)) - Lay.row.k(ty, String.eq(id, sel), id, title, Lay.row.note(b, Bool.pick(String, snoozed, U.Time.ago(U.Side.until(st, id), now), U.Time.ago(now, a))), Lay.row.nc(b, T.Th.faint()), + Lay.row.k(ty, String.eq(id, sel), id, U.Label.title(st, id, title), Lay.row.note(b, Bool.pick(String, snoozed, U.Time.ago(U.Side.until(st, id), now), U.Time.ago(now, a))), Lay.row.nc(b, T.Th.faint()), Lay.phase(U.Side.phase(st, t)), BG.Badge.rgb_of(st, p), c, y) # whether a row at y (32 tall) shows in c at all (one scrolled above the @@ -430,7 +430,7 @@ def Look.open(open: Bool, +ty: Ty, +st: M.State, +id: String, +rm: Bool, +c: D.C def Lay.project(+ty: Ty, r: U.SRow, +quiet: Bool, +open: Map<&2, Bool>, +st: M.State, +sel: String, +now: Nat, +look: String, +rm: String, +c: D.Clip, +y: U32) -> Col: match r: case U.SRow{_, M.Project{+id, title, _, _, _}, M.Shelves{pinned, active, settled, hidden, snoozed}}: - Lay.col.cat(Out.all([Out{Ty.text(ty, 2n, Ty.fit(ty, 2n, title, 180), c, 38, (y + 4 : U32), 28, Bool.pick(U32, quiet, T.Th.faint(), T.Th.dim())), [Lay.hit(0, y, 228, 32, "project", id)]}, + Lay.col.cat(Out.all([Out{Ty.text(ty, 2n, Ty.fit(ty, 2n, U.Label.title(st, id, title), 180), c, 38, (y + 4 : U32), 28, Bool.pick(U32, quiet, T.Th.faint(), T.Th.dim())), [Lay.hit(0, y, 228, 32, "project", id)]}, Out{Lay.icon(ty, BG.Badge.icon(st, id), c, 14, (y + 10 : U32), Bool.pick(U32, quiet, T.Th.faint(), BG.Badge.rgb_of(st, id))), [Lay.hit(12, (y + 4 : U32), 24, 28, "look", id)]}, Out{Lay.icon(ty, "plus", c, 236, (y + 10 : U32), T.Th.faint()), [Lay.hit(230, (y + 4 : U32), 28, 28, "new-thread", id)]}]), Lay.col.then(Look.open(String.eq(look, id), ty, st, id, String.eq(rm, id), c, (y + 32 : U32)), y0 => @@ -500,7 +500,7 @@ def Lay.arow.k(+ty: Ty, +on: Bool, id: String, title: String, +proj: String, +ba def Lay.arow.on(+ty: Ty, r: U.ARow, +st: M.State, +sel: String, +c: D.Clip, +y: U32) -> Out: match r: case U.ARow{_, _, +t, +pid, +pt, +faded}: - Lay.arow.k(ty, String.eq(M.Thread.id(t), sel), M.Thread.id(t), U.Active.title(t), pt, SB.Subs.badge(SB.Subs.count(st, M.Thread.id(t))), faded, Lay.phase(U.Side.phase(st, t)), BG.Badge.rgb_of(st, pid), c, y) + Lay.arow.k(ty, String.eq(M.Thread.id(t), sel), M.Thread.id(t), U.Label.title(st, M.Thread.id(t), U.Active.title(t)), pt, SB.Subs.badge(SB.Subs.count(st, M.Thread.id(t))), faded, Lay.phase(U.Side.phase(st, t)), BG.Badge.rgb_of(st, pid), c, y) def Lay.arow.id(r: U.ARow) -> String: match r: @@ -1481,7 +1481,7 @@ def Lay.header.k(+ty: Ty, +title: String, +branch: String, +icon: String, +tint: def Lay.header(+ty: Ty, t: M.Thread, +st: M.State, +x0: U32, +x1: U32, panel: Bool, term: Bool, +snoozed: Bool, del: Bool, +hy: U32) -> Out: match t: case M.Thread{+id, +p, +title, _, _, turn, _, +archived, override, +sa, +ua, pinned, _, +branch, _, _, _, _, _, _, _}: - Lay.header.k(ty, title, branch, BG.Badge.icon(st, p), BG.Badge.rgb_of(st, p), Lay.header.bs(turn, override, sa, ua, pinned, panel, archived, snoozed, del, Bool.not(String.is_empty(M.Fork.source(st, id))), term), x0, x1, hy) + Lay.header.k(ty, U.Label.title(st, id, title), branch, BG.Badge.icon(st, p), BG.Badge.rgb_of(st, p), Lay.header.bs(turn, override, sa, ua, pinned, panel, archived, snoozed, del, Bool.not(String.is_empty(M.Fork.source(st, id))), term), x0, x1, hy) def Lay.lines(+ty: Ty, ls: List<&2, String>, +c: D.Clip, +x: U32, +y: U32) -> Out: match ls: diff --git a/src/app/link.bend b/src/app/link.bend index b6b669a63f..df22044cc2 100644 --- a/src/app/link.bend +++ b/src/app/link.bend @@ -6,6 +6,16 @@ import ../core/utf8.bend as U import ../core/json.bend as J import ../core/cbor.bend as CB +# Resolution and TCP connect share a bounded deadline. The byte poll +# keeps any binary frame that arrives with the HTTP upgrade head intact. +def Link.connect(host: String, port: U32, ms: U32) -> IO(Result<&1, &1, U32 & String, Socket>): + import "./effects/link.c" + import "./effects/link.js" + +def Link.poll(s: Socket, ms: U32) -> IO(Socket & Result<&1, &1, U32 & String, Maybe<&2, List<&2, U32>>>): + import "./effects/link.c" + import "./effects/link.js" + # A link to another machine's hub, in the shape of a hub: the window sends # it S.Msg (Join once, then Rpc for each request, Leave to hang up) and # reads S.Wire from the channel it joined with, just as with its own hub. @@ -36,7 +46,7 @@ def Link.say(+out: Chan(S.Wire), +text: String) -> IO(Unit): # the link is down: say why (a note the window shows), then hang up def Link.down(+out: Chan(S.Wire), +why: String) -> IO(Unit): do IO: - Link.say(out, "{\"t\":\"update\",\"state\":" ++ "\"" ++ why ++ "\"}") + Link.say(out, J.Json.show(J.Json.obj([J.KV{"t", J.Str{"update"}}, J.KV{"state", J.Str{why}}, J.KV{"error", J.Str{why}}]))) Chan.close(S.Wire, out) # Writing: requests from the window become masked binary CBOR frames @@ -212,35 +222,56 @@ def Link.split.k(m: Maybe<&2, W.Split>, s: Socket, +inbox: Chan(S.Msg), +out: Ch case None{}: k(s) -def Link.head(fuel: Nat, s: Socket, +buf: List<&2, U32>, +inbox: Chan(S.Msg), +out: Chan(S.Wire)) -> IO(Unit): +def Link.unreachable(s: Socket, +inbox: Chan(S.Msg), +out: Chan(S.Wire)) -> IO(Unit): + do IO: + Socket.close(s) + IO.spawn(Unit, Link.drain(Link.fuel(), inbox)) + Link.down(out, "unreachable: no handshake from that machine within 5 seconds") + +def Link.head.got(r: Socket & Result<&1, &1, U32 & String, Maybe<&2, List<&2, U32>>>, +inbox: Chan(S.Msg), +out: Chan(S.Wire), k: Socket -> List<&2, U32> -> IO(Unit)) -> IO(Unit): + (s, res) = r + match res: + case Done{Some{Nil{}}}: + Link.unreachable(s, inbox, out) + case Done{Some{bs}}: + k(s, bs) + case _: + Link.unreachable(s, inbox, out) + +def Link.head(fuel: Nat, s: Socket, +buf: List<&2, U32>, +deadline: Nat, +inbox: Chan(S.Msg), +out: Chan(S.Wire)) -> IO(Unit): match fuel: case 0n: - do IO: - Socket.close(s) - IO.spawn(Unit, Link.drain(Link.fuel(), inbox)) - Link.down(out, "no answer from that machine") + Link.unreachable(s, inbox, out) case 1n+f: Link.split.k(W.Http.split(buf), s, inbox, out, - s2 => Link.more(s2, out, s3 => bs => Link.head(f, s3, List.append(&2, U32, buf, bs), inbox, out))) + s2 => IO.bind(Nat, Unit, IO.now(), now => IO.bind(Socket & Result<&1, &1, U32 & String, Maybe<&2, List<&2, U32>>>, Unit, + Link.poll(s2, U32.from_nat(Nat.sub(deadline, now))), r => Link.head.got(r, inbox, out, s3 => bs => Link.head(f, s3, List.append(&2, U32, buf, bs), deadline, inbox, out))))) def Link.sent(r: Socket & Result<&1, &1, U32 & String, Unit>, +inbox: Chan(S.Msg), +out: Chan(S.Wire)) -> IO(Unit): - (s, _) = r - Link.head(64n, s, Nil{}, inbox, out) + (s, res) = r + match res: + case Done{_}: + IO.bind(Nat, Unit, IO.now(), now => Link.head(64n, s, Nil{}, Nat.add(now, 5000n), inbox, out)) + case Fail{_}: + Link.unreachable(s, inbox, out) + +def Link.path(since: Nat, origin: String) -> String: + "/ws?since=" ++ Nat.show(since) ++ "&origin=" ++ origin ++ "&enc=cbor" -def Link.connected(r: Result<&1, &1, U32 & String, Socket>, +url: String, +inbox: Chan(S.Msg), +out: Chan(S.Wire)) -> IO(Unit): +def Link.connected(r: Result<&1, &1, U32 & String, Socket>, +url: String, since: Nat, origin: String, +inbox: Chan(S.Msg), +out: Chan(S.Wire)) -> IO(Unit): match r: case Done{s}: - IO.bind(Socket & Result<&1, &1, U32 & String, Unit>, Unit, H.Sock.send_text(s, W.Ws.request(Link.host(url), Link.port(url), "/ws?since=0&origin=&enc=cbor")), + IO.bind(Socket & Result<&1, &1, U32 & String, Unit>, Unit, H.Sock.send_text(s, W.Ws.request(Link.host(url), Link.port(url), Link.path(since, origin))), r2 => Link.sent(r2, inbox, out)) case Fail{_}: do IO: IO.spawn(Unit, Link.drain(Link.fuel(), inbox)) - Link.down(out, "cannot reach " ++ Link.host(url)) + Link.down(out, "unreachable: cannot resolve or reach " ++ Link.host(url)) def Link.joined(m: Maybe<&1, S.Msg>, +url: String, +inbox: Chan(S.Msg)) -> IO(Unit): match m: - case Some{S.Join{_, out, _, _}}: - IO.bind(Result<&1, &1, U32 & String, Socket>, Unit, TCP.connect(Link.host(url), Link.port(url)), r => Link.connected(r, url, inbox, out)) + case Some{S.Join{_, out, since, origin}}: + IO.bind(Result<&1, &1, U32 & String, Socket>, Unit, Link.connect(Link.host(url), Link.port(url), 5000), r => Link.connected(r, url, since, origin, inbox, out)) case _: IO.pure(Unit, Unit{}) diff --git a/src/app/main.bend b/src/app/main.bend index 9c2c3f0446..0c65815130 100644 --- a/src/app/main.bend +++ b/src/app/main.bend @@ -69,8 +69,46 @@ type ShellMsg is Data: # which hub the window talks to: its own (url "") or another machine's # through a link; each connection gets its own tag so messages from one # already left are dropped +type TieKeep is Data: + TieKeep{pics: Map<&2, BM.Bitmap>, asked: Map<&2, Bool>, cache: List<&2, U.HubView>} + +# Keep the growing connection data behind a list, as Kept does for parks: +# the window's IO continuations must stay under the native arity limit. type Tie is Data: - Tie{home: Chan(S.Msg), url: String, tag: String, n: Nat, pics: Map<&2, BM.Bitmap>, asked: Map<&2, Bool>} + Tie{home: Chan(S.Msg), url: String, tag: String, n: Nat, kept: List<&2, TieKeep>} + +def TieKeep.empty() -> List<&2, TieKeep>: + [TieKeep{Map.new(&2, BM.Bitmap), Map.new(&2, Bool), Nil{}}] + +def TieKeep.pics(ks: List<&2, TieKeep>) -> Map<&2, BM.Bitmap>: + match ks: + case TieKeep{ps, _, _} <> _: ps + case Nil{}: Map.new(&2, BM.Bitmap) + +def TieKeep.asked(ks: List<&2, TieKeep>) -> Map<&2, Bool>: + match ks: + case TieKeep{_, ak, _} <> _: ak + case Nil{}: Map.new(&2, Bool) + +def TieKeep.cache(ks: List<&2, TieKeep>) -> List<&2, U.HubView>: + match ks: + case TieKeep{_, _, cs} <> _: cs + case Nil{}: Nil{} + +def TieKeep.with_pics(ks: List<&2, TieKeep>, ps: Map<&2, BM.Bitmap>) -> List<&2, TieKeep>: + match ks: + case TieKeep{_, ak, cs} <> _: [TieKeep{ps, ak, cs}] + case Nil{}: [TieKeep{ps, Map.new(&2, Bool), Nil{}}] + +def TieKeep.with_asked(ks: List<&2, TieKeep>, ak: Map<&2, Bool>) -> List<&2, TieKeep>: + match ks: + case TieKeep{ps, _, cs} <> _: [TieKeep{ps, ak, cs}] + case Nil{}: [TieKeep{Map.new(&2, BM.Bitmap), ak, Nil{}}] + +def TieKeep.with_cache(ks: List<&2, TieKeep>, cs: List<&2, U.HubView>) -> List<&2, TieKeep>: + match ks: + case TieKeep{ps, ak, _} <> _: [TieKeep{ps, ak, cs}] + case Nil{}: [TieKeep{Map.new(&2, BM.Bitmap), Map.new(&2, Bool), cs}] # what one frame keeps for the next: its tiles, the timeline's memos # (the open thread's first, then the threads shown before it), the @@ -247,8 +285,8 @@ def Pics.size(+w: U32, +h: U32) -> String: def Tie.pics(t: Tie) -> Map<&2, BM.Bitmap>: match t: - case Tie{_, _, _, _, ps, _}: - ps + case Tie{_, _, _, _, ks}: + TieKeep.pics(ks) def Pics.sent.ok(got: U32 & U32 & List<&2, U32>, +q: Chan(ShellMsg), +tag: String, +path: String) -> IO(Unit): (w, rest) = got @@ -408,9 +446,9 @@ def Pics.noted(u: U.Ui, +path: String, +note: String) -> U.Ui: def Pics.store(a: Shell, +path: String, +note: String, m: Maybe<&2, BM.Bitmap>) -> IO(Shell): match a: - case Shell{+n, w, _, qd, q, i, Aux{ws, ty, Tie{home, url, tag, k, ps, ak}, fr}}: + case Shell{+n, w, _, qd, q, i, Aux{ws, ty, Tie{home, url, tag, k, +ks}, fr}}: Shell.later(Shell{N.Nui.with_ui(n, Pics.noted(N.Nui.ui(n), path, note)), w, True{}, qd, q, i, - Aux{ws, ty, Tie{home, url, tag, k, Shot.keep(Shot.ok(path, note), m, ps, path), ak}, fr}}) + Aux{ws, ty, Tie{home, url, tag, k, TieKeep.with_pics(ks, Shot.keep(Shot.ok(path, note), m, TieKeep.pics(ks), path))}, fr}}) # an image loaded (or failed) for this connection: keep it and redraw def Pics.got(current: Bool, a: Shell, +path: String, +note: String, m: Maybe<&2, BM.Bitmap>) -> IO(Shell): @@ -437,9 +475,10 @@ def Pics.uniq(ps: List<&2, String>, +acc: List<&2, String>) -> List<&2, String>: # images the frame shows that were never asked for start loading def Pics.ask(+hs: List<&2, N.Hit>, wa: Aux, +q: Chan(ShellMsg), +bp: String) -> IO(Aux): match wa: - case Aux{ws, ty, Tie{home, +url, +tag, n, ps, +ak}, fr}: + case Aux{ws, ty, Tie{home, +url, +tag, n, +ks}, fr}: + +ak = TieKeep.asked(ks) +fresh = Pics.uniq(Pics.new(hs, ak), Nil{}) - IO.bind(Unit, Aux, Pics.spawn(fresh, url, q, tag, bp), u => IO.pure(Aux, Aux{ws, ty, Tie{home, url, tag, n, ps, Pics.mark(fresh, ak)}, fr})) + IO.bind(Unit, Aux, Pics.spawn(fresh, url, q, tag, bp), u => IO.pure(Aux, Aux{ws, ty, Tie{home, url, tag, n, TieKeep.with_asked(ks, Pics.mark(fresh, ak))}, fr})) # no caret blinks under the lightbox (nothing else asks for frames then) def Lbv.blink(on: Bool, +n: N.Nui, +q: Chan(ShellMsg), +ms: Nat) -> IO(N.Nui): @@ -510,12 +549,12 @@ def Pics.over(hs: List<&2, N.Hit>, +pics: Map<&2, BM.Bitmap>, +done: List<&2, St def Tie.tag(t: Tie) -> String: match t: - case Tie{_, _, g, _, _, _}: + case Tie{_, _, g, _, _}: g def Tie.url(t: Tie) -> String: match t: - case Tie{_, u, _, _, _, _}: + case Tie{_, u, _, _, _}: u # the hub to join: this machine's own, or a link to another @@ -526,21 +565,30 @@ def Shell.hub(local: Bool, +home: Chan(S.Msg), +url: String) -> IO(Chan(S.Msg)): case False{}: LK.Link.start(url) -def Shell.joined(+i2: Chan(S.Msg), +q: Chan(ShellMsg), +tag: String) -> IO(Unit): +def Shell.joined(+i2: Chan(S.Msg), +q: Chan(ShellMsg), +tag: String, since: Nat, origin: String) -> IO(Unit): do IO: +out : Chan(S.Wire) <- Chan.new(S.Wire, 8192) - ok : Bool <- Chan.send(S.Msg, i2, S.Join{0, out, 0n, ""}) + ok : Bool <- Chan.send(S.Msg, i2, S.Join{0, out, since, origin}) IO.spawn(Unit, Fwd.loop(S.Fuel.forever(), out, q, tag)) +# Save the departing installed state before an input step replaces it. +# The list behind Tie bounds both retained read models and record arity. +def Shell.remember(a: Shell) -> Shell: + match a: + case Shell{+nu, w, d, qd, q, i, Aux{ws, ty, Tie{home, url, tag, n, +ks}, fr}}: + Shell{nu, w, d, qd, q, i, Aux{ws, ty, Tie{home, url, tag, n, TieKeep.with_cache(ks, U.HubCache.save(N.Nui.ui(nu), TieKeep.cache(ks)))}, fr}} + def Shell.connect.k(t: Tie, a: Shell, +url: String) -> IO(Shell): match t a: - case Tie{+home, _, _, +n, _, _} Shell{nu, w, d, qd, +q, +i, Aux{ws, ty, _, fr}}: + case Tie{+home, _, _, +n, ks} Shell{nu, w, d, qd, +q, +i, Aux{ws, ty, _, fr}}: do IO: + +cache : List<&2, U.HubView> = TieKeep.cache(ks) ok : Bool <- Chan.send(S.Msg, i, S.Leave{0}) +tag : String = url ++ "#" ++ Nat.show(n) + +nu2 : N.Nui = N.Nui.connection(nu, tag, U.HubCache.get(cache, url)) +i2 : Chan(S.Msg) <- Shell.hub(String.is_empty(url), home, url) - Shell.joined(i2, q, tag) - return Shell{N.Nui.desk.reset(nu), w, True{}, qd, q, i2, Aux{ws, ty, Tie{home, url, tag, 1n+n, Map.new(&2, BM.Bitmap), Map.new(&2, Bool)}, fr}} + Shell.joined(i2, q, tag, U.Ui.seq(N.Nui.ui(nu2)), U.Ui.info(N.Nui.ui(nu2), "origin")) + return Shell{N.Nui.desk.reset(nu2), w, True{}, qd, q, i2, Aux{ws, ty, Tie{home, url, tag, 1n+n, [TieKeep{Map.new(&2, BM.Bitmap), Map.new(&2, Bool), cache}]}, Kept{TI.FNone{}, Nil{}, CA.Cats.none(), [N.Park.init()], BV.Bv.wraps.none()}}} def Shell.retry.later(+q: Chan(ShellMsg), +tag: String) -> IO(Unit): do IO: @@ -566,7 +614,7 @@ def Shell.dirty(a: Shell) -> IO(Shell): def Shell.adopt(current: Bool, a: Shell, st: M.State, +head: String) -> IO(Shell): match current a: case True{} Shell{n, w, _, qd, q, i, wa}: - Shell.later(Shell{N.Nui.adopt(N.Nui.recv(n, Maybe.default(&2, J.Json, J.Json.parse(head), J.Null{})), st), w, True{}, qd, q, i, wa}) + Shell.later(Shell{N.Nui.adopt(n, st, Maybe.default(&2, J.Json, J.Json.parse(head), J.Null{})), w, True{}, qd, q, i, wa}) case False{} _: IO.pure(Shell, a) @@ -620,11 +668,14 @@ def Aux.tie(a: Aux) -> Tie: # talk to the hub at url ("" is this machine's): leave the one now joined, # join the other; its log replaces what the window shows -def Shell.connect(a: Shell, +url: String) -> IO(Shell): +def Shell.connect.go(a: Shell, +url: String) -> IO(Shell): match a: case Shell{n, w, d, qd, q, i, Aux{ws, ty, +ti, fr}}: Shell.connect.k(ti, Shell{n, w, d, qd, q, i, Aux{ws, ty, ti, fr}}, url) +def Shell.connect(a: Shell, url: String) -> IO(Shell): + Shell.connect.go(Shell.remember(a), url) + # Commands # -------- @@ -763,10 +814,10 @@ def Shell.gone(current: Bool, local: Bool, a: Shell, +tag: String) -> IO(Shell): match current local a: case True{} True{} _: IO.die(Shell, 0, "") - case True{} False{} Shell{n, w, d, qd, +q, i, wa}: + case True{} False{} Shell{+n, w, d, qd, +q, i, wa}: do IO: IO.spawn(Unit, Shell.retry.later(q, tag)) - return Shell{n, w, d, qd, q, i, wa} + return Shell{N.Nui.with_ui(n, U.Ui.online(N.Nui.ui(n), False{})), w, True{}, qd, q, i, wa} case False{} _ _: IO.pure(Shell, a) @@ -785,11 +836,14 @@ def Shell.cmds(cs: List<&2, N.NCmd>, a: Shell) -> IO(Shell): IO.bind(Shell, Shell, Shell.cmd(a, c), a2 => Shell.cmds(t, a2)) # fold a step into the app: its state, its commands, maybe a frame -def Shell.step(a: Shell, s: N.Step) -> IO(Shell): +def Shell.step.go(a: Shell, s: N.Step) -> IO(Shell): match a s: case Shell{_, w, d, qd, q, i, wa} N.Step{n, cs, dirty}: IO.bind(Shell, Shell, Shell.cmds(cs, Shell{n, w, Bool.or(d, dirty), qd, q, i, wa}), Shell.dirty) +def Shell.step(a: Shell, s: N.Step) -> IO(Shell): + Shell.step.go(Shell.remember(a), s) + # an action come due runs as a click would def Shell.due(a: Shell, +action: String, +value: String) -> IO(Shell): match a: @@ -1687,12 +1741,24 @@ def Aux.with_ty(a: Aux, t: L.Ty) -> Aux: case Aux{ws, _, ti, k}: Aux{ws, t, ti, k} +# Match before calling Lay.memos: a hidden timeline does no wrapping. +def Shell.memos(visible: Bool, ty: L.Ty, n: N.Nui, ms: List<&2, L.TLMemo>) -> List<&2, L.TLMemo>: + match visible: + case True{}: + L.Lay.memos(ty, n, ms) + case False{}: + ms + +# Keep the repeated Nui use outside the frame's large IO continuation. +def Shell.memos.of(ty: L.Ty, +n: N.Nui, ms: List<&2, L.TLMemo>) -> List<&2, L.TLMemo>: + Shell.memos(U.Ui.timeline(N.Nui.ui(n)), ty, n, ms) + # the frame again with the glyphs it wanted in the cache: the timeline's # memos and the posts' lines are made again (they were measured without # them) def Shell.paint.again(+ty: L.Ty, +cats: Map<&2, CA.CatF>, +oh: List<&2, N.Hit>, +n: N.Nui, w: Window, +q: Chan(ShellMsg), +i: Chan(S.Msg), +wa: Aux, +epoch: Nat, +ms: Nat, +t0: U32, +t2: U32) -> IO(Shell): do IO: - +memos : List<&2, L.TLMemo> = L.Lay.memos(ty, n, Nil{}) + +memos : List<&2, L.TLMemo> = Shell.memos.of(ty, n, Nil{}) +wraps : Map<&2, BV.PW> = BV.Bv.wraps(ty, n, BV.Bv.wraps.none()) +o : L.Out = BV.Bv.frame.w(ty, L.TL.memos.now(memos), cats, wraps, n) +k : Nat = Lay.nops(o) @@ -1728,7 +1794,7 @@ def Shell.paint.m(+memos: List<&2, L.TLMemo>, +oh: List<&2, N.Hit>, +n: N.Nui, w def Shell.paint.go(+oh: List<&2, N.Hit>, +n: N.Nui, w: Window, +q: Chan(ShellMsg), +i: Chan(S.Msg), +wa: Aux, +epoch: Nat, +ms: Nat) -> IO(Shell): do IO: +t0 : U32 <- PS.Perf.us() - +memos : List<&2, L.TLMemo> = L.Lay.memos(Aux.ty(wa), n, Aux.memos(wa)) + +memos : List<&2, L.TLMemo> = Shell.memos.of(Aux.ty(wa), n, Aux.memos(wa)) +t1 : U32 <- PS.Perf.lap("ui.memo", t0) Shell.paint.m(memos, oh, n, w, q, i, wa, epoch, ms, t0, t1) @@ -1941,7 +2007,7 @@ def Shell.twin(r: Window & Window, +inbox: Chan(S.Msg), +drafts: String, +fq: Ch got : Maybe<&1, L.Ty> <- Chan.recv(L.Ty, fq) ty : L.Ty <- Shell.fonts(got) ui : U.Ui <- Drafts.load(U.Ui.init(U32.to_nat(t)), drafts) - a : Shell <- Shell.later(Shell{N.Nui.init(ui, 1400, 900), pw, True{}, False{}, q, inbox, Aux{Nil{}, ty, Tie{inbox, "", "#0", 1n, Map.new(&2, BM.Bitmap), Map.new(&2, Bool)}, Kept{TI.FNone{}, Nil{}, CA.Cats.none(), [N.Park.init()], BV.Bv.wraps.none()}}}) + a : Shell <- Shell.later(Shell{N.Nui.init(ui, 1400, 900), pw, True{}, False{}, q, inbox, Aux{Nil{}, ty, Tie{inbox, "", "#0", 1n, TieKeep.empty()}, Kept{TI.FNone{}, Nil{}, CA.Cats.none(), [N.Park.init()], BV.Bv.wraps.none()}}}) Shell.loop(S.Fuel.forever(), a) def Shell.opened(r: Result<&1, &1, U32 & String, Window>, +inbox: Chan(S.Msg), +drafts: String, +fq: Chan(L.Ty)) -> IO(Unit): diff --git a/src/app/nui.bend b/src/app/nui.bend index e8aec5f376..1e707626d9 100644 --- a/src/app/nui.bend +++ b/src/app/nui.bend @@ -696,8 +696,21 @@ def Jump.settle(s: Step) -> Step: def Nui.unpick(+n: Nui) -> Nui: Bool.pick(Nui, Bool.and(String.eq(Nui.focus_of(n), "proj"), Bool.not(U.Ui.flag(Nui.ui(n), "proj"))), Nui.with_focus(n, "composer"), n) +def Nui.unremote.k(promoted: Bool, n: Nui) -> Nui: + match promoted n: + case True{} Nui{+u, w, h, mx, my, sd, _, _, _, pa, hi, ms, vw}: + Nui{u, w, h, mx, my, sd, 0, String.length(U.Ui.draft_text(u)), "composer", pa, hi, ms, vw} + case False{} _: + n + +# A reported chat gaining its mirror replaces the form with the normal +# composer. Keep keyboard focus on the visible field after that change. +def Nui.unremote(+n: Nui) -> Nui: + Nui.unremote.k(Bool.and(String.eq(Nui.focus_of(n), Fld.alias("remote-send")), + Bool.and(String.is_empty(U.Ui.scratch(Nui.ui(n), "@remote")), Bool.not(String.is_empty(U.Ui.scratch(Nui.ui(n), "@bot"))))), n) + def Nui.recv(n: Nui, +o: J.Json) -> Nui: - Nui.unpick(Nui.recv.k(n, o)) + Nui.unpick(Nui.unremote(Nui.recv.k(n, o))) def Undo.key(+n: Nui, +w: String) -> String: "@" ++ w ++ ":" ++ U.Ui.sel(Nui.ui(n)) @@ -1367,10 +1380,10 @@ def Vw.stale(v: V.Vw) -> V.Vw: case V.Vw{s, vi, f, r, d, lx, ly, b, _, cu, vx}: V.Vw{s, vi, f, r, d, lx, ly, b, False{}, cu, vx} -def Nui.adopt.k(n: Nui, st: M.State) -> Nui: +def Nui.adopt.k(n: Nui, st: M.State, head: J.Json) -> Nui: match n: case Nui{u, w, h, mx, my, sd, sc, ca, fo, pa, hi, ms, vw}: - Nui{U.Ui.adopt(u, st), w, h, mx, my, sd, sc, ca, fo, pa, hi, ms, vw} + Nui{U.Ui.adopt_log(u, st, head), w, h, mx, my, sd, sc, ca, fo, pa, hi, ms, vw} def Nui.adopt.theme(changed: Bool, +n: Nui) -> Nui: match changed: @@ -1381,8 +1394,15 @@ def Nui.adopt.theme(changed: Bool, +n: Nui) -> Nui: # the hub's read model, handed over (main.bend's Shell.adopt); a new theme # draws the viewer's base again (its plain part is the window's ground) -def Nui.adopt(+n: Nui, +st: M.State) -> Nui: - Nui.adopt.theme(Bool.not(String.eq(M.Setting.get(st, "ui.theme", ""), M.Setting.get(U.Ui.state.of(Nui.ui(n)), "ui.theme", ""))), Nui.adopt.k(n, st)) +def Nui.adopt(+n: Nui, +st: M.State, head: J.Json) -> Nui: + Nui.adopt.theme(Bool.not(String.eq(M.Setting.get(st, "ui.theme", ""), M.Setting.get(U.Ui.state.of(Nui.ui(n)), "ui.theme", ""))), Nui.adopt.k(n, st, head)) + +# A new machine drops native focus, scroll, hits, selections and scene as +# well as core selectors. A cached read model is safe to display at once. +def Nui.connection(n: Nui, +tag: String, cached: Maybe<&2, U.HubView>) -> Nui: + match n: + case Nui{u, w, h, _, _, _, _, _, _, _, _, _, _}: + Nui.init(U.Ui.cached(cached, U.Ui.connection(u, tag)), w, h) # a board file arrived for a project root: same root merges (only what # changed fades), a new root starts over (the whole board fades in) diff --git a/src/core/bot.bend b/src/core/bot.bend index 4c85a25ad8..5d2ef72852 100644 --- a/src/core/bot.bend +++ b/src/core/bot.bend @@ -392,6 +392,19 @@ def World.bots(+w: World) -> List<&2, Bot>: case World{bs, _, _, _, _, _, _, _, _, _, _}: List.sort(~Bot, ~Bot.before, List.filter(~Bot, ~Bot.live, Map.values(&2, Bot, bs))) +def Bot.here(b: Bot) -> Bool: + match b: + case Bot{+i, _, _, _, _, _, d}: + Bool.and(Bool.not(d), Bool.not(String.contains(i, "~"))) + +# this machine's own live bots, oldest first: not those a client mirrors +# from a linked machine (far.bend names them "~"), which rooms, +# mentions and pickers reach as name@machine instead +def World.bots.here(+w: World) -> List<&2, Bot>: + match w: + case World{bs, _, _, _, _, _, _, _, _, _, _}: + List.sort(~Bot, ~Bot.before, List.filter(~Bot, ~Bot.here, Map.values(&2, Bot, bs))) + def Bot.named.k(+b: Bot, +name: String) -> Bool: Bool.and(Bot.live(b), String.eq(String.to_lower(Bot.name(b)), String.to_lower(name))) diff --git a/src/core/bothub.bend b/src/core/bothub.bend index 6cea8f5606..4e33ce321a 100644 --- a/src/core/bothub.bend +++ b/src/core/bothub.bend @@ -10,6 +10,196 @@ import ./ring.bend as RG import ./space.bend as SP import ./refs.bend as RF +# Remote bot directories. A result replaces just its peer; a failure +# keeps that peer's rows and their last-seen timestamps, marked away. +# Sort by peer id and bot name so arrival order never changes the UI. +def Dir.item(peer: String, pname: String, look: Nat, name: String, state: String, seen: Nat) -> J.Json: + J.Json.obj([J.KV{"name", J.Str{name}}, J.KV{"peer", J.Str{peer}}, J.KV{"peerName", J.Str{pname}}, J.KV{"look", J.Json.nat(look)}, + J.KV{"state", J.Str{state}}, J.KV{"seen", J.Json.nat(seen)}]) + +# Change only the state field, preserving even fields newer clients add. +def Dir.away.fields(fs: J.Json) -> J.Json: + match fs: + case J.Field{+k, +v, t}: + J.Field{k, Bool.pick(J.Json, String.eq(k, "state"), J.Str{"away"}, v), Dir.away.fields(t)} + case _: + J.End{} + +def Dir.away.one(o: J.Json) -> J.Json: + match o: + case J.Obj{fs}: + J.Obj{Dir.away.fields(fs)} + case _: + J.Null{} + +def Dir.changed(mine: Bool, +o: J.Json) -> J.Json: + match mine: + case True{}: + Dir.away.one(o) + case False{}: + o + +def Dir.away(xs: List<&2, J.Json>, +peer: String) -> List<&2, J.Json>: + match xs: + case Nil{}: + Nil{} + case +o <> t: + Dir.changed(String.eq(J.Json.get_str(o, "peer", ""), peer), o) <> Dir.away(t, peer) + +def Dir.other(xs: List<&2, J.Json>, +peer: String) -> List<&2, J.Json>: + match xs: + case Nil{}: + Nil{} + case +o <> t: + BT.Post.keep(J.Json, Bool.not(String.eq(J.Json.get_str(o, "peer", ""), peer)), o, Dir.other(t, peer)) + +def Dir.key(+o: J.Json) -> String: + J.Json.get_str(o, "peer", "") ++ ":" ++ J.Json.get_str(o, "name", "") + +def Dir.le(a: J.Json, b: J.Json) -> Bool: + String.is_le(Dir.key(a), Dir.key(b)) + +def Dir.sort(xs: List<&2, J.Json>) -> List<&2, J.Json>: + List.sort(~J.Json, ~Dir.le, xs) + +def Dir.merge(m: Maybe<&2, List<&2, J.Json>>, prev: List<&2, J.Json>, +peer: String) -> List<&2, J.Json>: + match m: + case Some{xs}: + Dir.sort(List.append(&2, J.Json, Dir.other(prev, peer), xs)) + case None{}: + Dir.away(prev, peer) + +def Dir.accept(current: Bool, result: Maybe<&2, List<&2, J.Json>>, prev: List<&2, J.Json>, +peer: String) -> List<&2, J.Json>: + match current: + case True{}: + Dir.merge(result, prev, peer) + case False{}: + prev + +def Dir.request.one(m: Maybe<&2, BT.Peer>) -> List<&2, BT.Peer>: + match m: + case Some{p}: + [p] + case None{}: + Nil{} + +def Dir.request.k(all: Bool, ps: List<&2, BT.Peer>, +peer: String) -> List<&2, BT.Peer>: + match all: + case True{}: + ps + case False{}: + Dir.request.one(BT.Peer.by.id(ps, peer)) + +def Dir.request(ps: List<&2, BT.Peer>, +peer: String) -> List<&2, BT.Peer>: + Dir.request.k(String.is_empty(peer), ps, peer) + +# Only known away rows make an inbound request a return signal. Empty +# directories are valid and must not create a reciprocal polling loop. +def Dir.stale(xs: List<&2, J.Json>, +peer: String) -> Bool: + match xs: + case Nil{}: + False{} + case +o <> t: + Bool.or(Bool.and(String.eq(J.Json.get_str(o, "peer", ""), peer), String.eq(J.Json.get_str(o, "state", ""), "away")), Dir.stale(t, peer)) + +def Dir.online(ms: List<&2, J.Json>, +name: String, +url: String) -> Bool: + match ms: + case Nil{}: + False{} + case +o <> t: + Bool.or(Bool.or(String.eq(J.Json.get_str(o, "url", ""), url), String.eq(J.Json.get_str(o, "name", ""), name)), Dir.online(t, name, url)) + +def Dir.returned(ps: List<&2, BT.Peer>, +before: List<&2, J.Json>, +after: List<&2, J.Json>) -> List<&2, BT.Peer>: + match ps: + case Nil{}: + Nil{} + case BT.Peer{+i, +n, +u, +ts, +r} <> t: + BT.Post.keep(BT.Peer, Bool.and(Bool.not(Dir.online(before, n, u)), Dir.online(after, n, u)), BT.Peer{i, n, u, ts, r}, Dir.returned(t, before, after)) + +def Dir.linked(xs: List<&2, J.Json>, +ps: List<&2, BT.Peer>) -> List<&2, J.Json>: + match xs: + case Nil{}: + Nil{} + case +o <> t: + BT.Post.keep(J.Json, Maybe.is_some(&2, BT.Peer, BT.Peer.by.id(ps, J.Json.get_str(o, "peer", ""))), o, Dir.linked(t, ps)) + +def Dir.all_away(xs: List<&2, J.Json>) -> List<&2, J.Json>: + match xs: + case Nil{}: + Nil{} + case o <> t: + Dir.away.one(o) <> Dir.all_away(t) + +def Dir.cached(xs: List<&2, J.Json>, +ps: List<&2, BT.Peer>) -> List<&2, J.Json>: + Dir.sort(Dir.all_away(Dir.linked(xs, ps))) + +# The hub owns this queue. A peer occurs once across active and pending; +# triggers during its fetch are coalesced. Slots are filled after every +# completion rather than waiting for the slowest member of a batch. +type DirWork is Data: + DirWork{active: List<&2, String>, pending: List<&2, String>} + +def DirWork.empty() -> DirWork: + DirWork{Nil{}, Nil{}} + +def DirWork.add.k(known: Bool, active: List<&2, String>, pending: List<&2, String>, peer: String) -> DirWork: + match known: + case True{}: + DirWork{active, pending} + case False{}: + DirWork{active, List.append(&2, String, pending, [peer])} + +def DirWork.add(w: DirWork, +peer: String) -> DirWork: + match w: + case DirWork{+a, +q}: + DirWork.add.k(Bool.or(List.contains(~String, ~String.eq, a, peer), List.contains(~String, ~String.eq, q, peer)), a, q, peer) + +def DirWork.queue(ps: List<&2, BT.Peer>, w: DirWork) -> DirWork: + match ps: + case Nil{}: + w + case BT.Peer{i, _, _, _, _} <> t: + DirWork.queue(t, DirWork.add(w, i)) + +def DirWork.remove(xs: List<&2, String>, +peer: String) -> List<&2, String>: + match xs: + case Nil{}: + Nil{} + case +h <> t: + BT.Post.keep(String, Bool.not(String.eq(h, peer)), h, DirWork.remove(t, peer)) + +def DirWork.finish(w: DirWork, +peer: String) -> DirWork: + match w: + case DirWork{a, q}: + DirWork{DirWork.remove(a, peer), q} + +type DirPlan is Data: + DirPlan{work: DirWork, launch: List<&2, String>} + +def DirPlan.work(p: DirPlan) -> DirWork: + match p: + case DirPlan{w, _}: + w + +def DirPlan.launch(p: DirPlan) -> List<&2, String>: + match p: + case DirPlan{_, xs}: + xs + +def DirWork.fill(fuel: Nat, w: DirWork, launch: List<&2, String>) -> DirPlan: + match fuel w: + case 0n _: + DirPlan{w, List.reverse(&2, String, launch)} + case 1n+f DirWork{a, Nil{}}: + DirPlan{DirWork{a, Nil{}}, List.reverse(&2, String, launch)} + case 1n+f DirWork{a, +i <> q}: + DirWork.fill(f, DirWork{i <> a, q}, i <> launch) + +def DirWork.plan(+w: DirWork) -> DirPlan: + match w: + case DirWork{+a, q}: + DirWork.fill(Nat.sub(4n, List.length(&2, String, a)), DirWork{a, q}, Nil{}) + # Bots, as a decider (docs/bots.md). Client requests ("bots.*"), the bots' # own MCP tools, messages between bots, routines, webhooks and messages # from linked machines all come in here and leave as changes and effects, diff --git a/src/core/botui.bend b/src/core/botui.bend index 5a3f1a9ba1..a5293a30f7 100644 --- a/src/core/botui.bend +++ b/src/core/botui.bend @@ -5,6 +5,7 @@ import ./client.bend as U import ./bot.bend as BT import ./space.bend as SP import ./once.bend as O +import ./far.bend as FR # What the bots' screens show, for every client (the window, the web and # the phones draw from these; docs/bots.md). The sidebar's cats with their @@ -95,6 +96,17 @@ def BU.remote.sel(+u: U.Ui) -> String: def BU.row.m(+mood: String, i: String, n: String, th: String, lk: Nat, sel: Bool) -> BRow: BRow{i, n, th, lk, mood, Mood.note(mood), sel, False{}, "", ""} +def BU.row.far.m(+mood: String, i: String, n: String, th: String, lk: Nat, sel: Bool, pe: String, l: String) -> BRow: + BRow{i, n, th, lk, mood, Mood.note(mood), sel, True{}, pe, l} + +# what choosing a row does: a bot with a thread here (this machine's, or +# one mirrored from its own) opens as a bot ("bot"); a bot only reported +# by a linked machine is a conversation ("remote") +def BU.act(r: BRow) -> String: + match r: + case BRow{_, _, +th, _, _, _, _, _, _, _}: + Bool.pick(String, String.is_empty(th), "remote", "bot") + # Each bot once # ------------- # Every list of bots keys each one by what it stands for and keeps the @@ -337,6 +349,13 @@ def BU.view(+u: U.Ui) -> View: def BU.tabs() -> List<&2, String>: ["chat", "space", "browser", "memory", "routines", "hooks", "settings"] +# a bot's tabs: all of them for a bot here; for one mirrored from a linked +# machine (far.bend) what its machine shares: its chat, space, memory and +# routines. Its browser, webhooks and settings stay on its machine (law +# far_bot_tabs). +def BU.tabs.of(+id: String) -> List<&2, String>: + Bool.pick(List<&2, String>, FR.Fr.is(id), ["chat", "space", "memory", "routines"], BU.tabs()) + def BU.tab.label(+t: String) -> String: Bool.pick(String, String.eq(t, "chat"), "Chat", Bool.pick(String, String.eq(t, "space"), "Space", @@ -345,10 +364,21 @@ def BU.tab.label(+t: String) -> String: Bool.pick(String, String.eq(t, "routines"), "Routines", Bool.pick(String, String.eq(t, "hooks"), "Webhooks", "Settings")))))) +def BU.tab.has(ts: List<&2, String>, +t: String) -> Bool: + match ts: + case Nil{}: + False{} + case x <> r: + Bool.or(String.eq(x, t), BU.tab.has(r, t)) + # the bot's tab ("chat" until one is picked) def BU.tab(+u: U.Ui) -> String: Bool.pick(String, String.is_empty(U.Ui.scratch(u, "@btab")), "chat", U.Ui.scratch(u, "@btab")) +# the tab shown: the one picked, when this bot has it, else its chat +def BU.tab.of(+u: U.Ui, +id: String) -> String: + Bool.pick(String, BU.tab.has(BU.tabs.of(id), BU.tab(u)), BU.tab(u), "chat") + def BU.space.none(paged: Bool, +name: String) -> SP.Space: match paged: case True{}: @@ -433,7 +463,7 @@ def BU.picks.gone(ms: List<&2, String>, +ps: List<&2, BT.Peer>) -> List<&2, O.Ky # this machine's bots, then those on linked machines ("name@machine"), # then members elsewhere no machine lists, before each is kept once def BU.picks.all(+u: U.Ui, +ms: List<&2, String>) -> List<&2, O.Ky>: - List.append(&2, O.Ky, BU.picks.go(BT.World.bots(M.State.bots(U.Ui.state(u))), BU.peers.of(u), ms, BT.World.bots(M.State.bots(U.Ui.state(u)))), + List.append(&2, O.Ky, BU.picks.go(BT.World.bots.here(M.State.bots(U.Ui.state(u))), BU.peers.of(u), ms, BT.World.bots.here(M.State.bots(U.Ui.state(u)))), List.append(&2, O.Ky, BU.picks.far(BU.remote.all(u), BU.peers.of(u), ms), BU.picks.gone(ms, BU.peers.of(u)))) def BU.picks.src(+u: U.Ui) -> List<&2, O.Ky>: @@ -488,20 +518,58 @@ def Mood.bot(+st: M.State, +now: Nat, b: BT.Bot) -> String: case BT.Bot{+i, _, +th, _, _, _, _}: Mood.bot.t(U.Ui.thread(st, th), st, now, BT.World.talked_at(M.State.bots(st), i)) -def BU.row(+st: M.State, +now: Nat, +sel: String, +all: List<&2, BT.Bot>, b: BT.Bot) -> BRow: +# a bot's mood, "away" while the machine it lives on does not answer +def BU.mood.far(away: Bool, +st: M.State, +now: Nat, b: BT.Bot) -> String: + match away: + case True{}: + "away" + case False{}: + Mood.bot(st, now, b) + +def BU.mood(+u: U.Ui, +b: BT.Bot) -> String: + BU.mood.far(U.Ui.far.away(u, BT.Bot.id(b)), U.Ui.state(u), U.Side.now(u), b) + +# this machine's own bots (not those mirrored from a linked machine) +def BU.here.k(far: Bool, b: BT.Bot, rest: List<&2, BT.Bot>) -> List<&2, BT.Bot>: + match far: + case True{}: + rest + case False{}: + b <> rest + +def BU.here(bs: List<&2, BT.Bot>) -> List<&2, BT.Bot>: + match bs: + case Nil{}: + Nil{} + case +b <> t: + BU.here.k(FR.Fr.is(BT.Bot.id(b)), b, BU.here(t)) + +def BU.row.here(+st: M.State, +now: Nat, +sel: String, +all: List<&2, BT.Bot>, b: BT.Bot) -> BRow: match b: case BT.Bot{+i, +n, +th, +lk, pe, c, d}: BU.row.m(Mood.bot(st, now, BT.Bot{i, n, th, lk, pe, c, d}), i, BU.label(Nat.is_gt(BU.named(all, n), 1n), n, i), th, lk, String.eq(i, sel)) -def BU.rows.go(bs: List<&2, BT.Bot>, +st: M.State, +now: Nat, +sel: String, +all: List<&2, BT.Bot>) -> List<&2, BRow>: +# a bot on a linked machine whose threads are mirrored here (far.bend): a +# bot like any other, keyed as its machine reports it (its name at the +# link), so the report of it is the same entry; away while its machine is +def BU.row.far(+u: U.Ui, +st: M.State, +now: Nat, +sel: String, b: BT.Bot) -> BRow: + match b: + case BT.Bot{+i, +n, +th, +lk, pe, c, d}: + BU.row.far.m(BU.mood.far(U.Ui.far.away(u, i), st, now, BT.Bot{i, n, th, lk, pe, c, d}), i, n, th, lk, String.eq(i, sel), + U.Ui.far.name(u, i), FR.Fr.link(i)) + +def BU.row(+u: U.Ui, +st: M.State, +now: Nat, +sel: String, +all: List<&2, BT.Bot>, +b: BT.Bot) -> BRow: + Bool.pick(BRow, FR.Fr.is(BT.Bot.id(b)), BU.row.far(u, st, now, sel, b), BU.row.here(st, now, sel, all, b)) + +def BU.rows.go(bs: List<&2, BT.Bot>, +u: U.Ui, +st: M.State, +now: Nat, +sel: String, +all: List<&2, BT.Bot>) -> List<&2, BRow>: match bs: case Nil{}: Nil{} case b <> t: - BU.row(st, now, sel, all, b) <> BU.rows.go(t, st, now, sel, all) + BU.row(u, st, now, sel, all, b) <> BU.rows.go(t, u, st, now, sel, all) def BU.rows.here(+u: U.Ui, +bs: List<&2, BT.Bot>) -> List<&2, BRow>: - BU.rows.go(bs, U.Ui.state(u), U.Side.now(u), BU.bot.sel(u), bs) + BU.rows.go(bs, u, U.Ui.state(u), U.Side.now(u), BU.bot.sel(u), BU.here(bs)) # the sidebar's cats before each is kept once: this machine's bots (oldest # first), then those the linked machines report @@ -519,3 +587,131 @@ def BU.rows(+u: U.Ui) -> List<&2, BRow>: # only while one does) def BU.moving(+u: U.Ui) -> Bool: BU.moving.go(BU.rows(u)) + +def BU.row.title.k(remote: Bool, +n: String, +pe: String) -> String: + match remote: + case True{}: + Bool.pick(String, String.is_empty(pe), n, n ++ " · " ++ pe) + case False{}: + n + +# A bot's view +# ------------ +# Every bot shows in one view whatever machine it lives on: its cat, its +# name with where it lives ("Kit · box", U.Ui.far.title), its mood, its +# tabs, then the tab. A bot mirrored from a linked machine (far.bend) is a +# bot like one here, less what stays on its machine: its browser, webhooks +# and settings (BU.tabs.of), its viewer (files on that machine), and +# changes to its memory and routines (only Fr.methods reach its hub: it can +# be written to, stopped, answered, and a routine run). A bot only reported +# by a linked machine (VRemote) is the same view with one tab, Chat: the +# person's conversation with it (laws bot_head_*). + +# a row's name as the list shows it: a bot elsewhere with its machine +def BU.row.title(r: BRow) -> String: + match r: + case BRow{_, +n, _, _, _, _, _, rem, +pe, _}: + BU.row.title.k(rem, n, pe) + +# whether a bot's header offers its viewer (its thread's files): only for +# a bot here +def BU.viewer.of(+id: String) -> Bool: + Bool.not(FR.Fr.is(id)) + +# whether a bot's memory and routines can be changed from here (Forget, +# Edit, Delete, On/Off, a new routine): only a bot here. Run now reaches a +# mirrored bot's hub too. +def BU.owns(+id: String) -> Bool: + Bool.not(FR.Fr.is(id)) + +type Head is Data: + Head{title: String, look: Nat, mood: String, note: String, tabs: List<&2, String>, tab: String, viewer: Bool, owns: Bool} + +def Head.title(h: Head) -> String: + match h: + case Head{t, _, _, _, _, _, _, _}: + t + +def Head.tabs(h: Head) -> List<&2, String>: + match h: + case Head{_, _, _, _, ts, _, _, _}: + ts + +def Head.tab(h: Head) -> String: + match h: + case Head{_, _, _, _, _, t, _, _}: + t + +def Head.viewer(h: Head) -> Bool: + match h: + case Head{_, _, _, _, _, _, v, _}: + v + +def Head.owns(h: Head) -> Bool: + match h: + case Head{_, _, _, _, _, _, _, o}: + o + +def Head.look(h: Head) -> Nat: + match h: + case Head{_, l, _, _, _, _, _, _}: + l + +def Head.mood(h: Head) -> String: + match h: + case Head{_, _, m, _, _, _, _, _}: + m + +def Head.note(h: Head) -> String: + match h: + case Head{_, _, _, n, _, _, _, _}: + n + +# a bot's header: a bot with a thread (remote False), or one only reported +# by a linked machine (remote True: Chat alone) +def BU.head.m(remote: Bool, +u: U.Ui, +id: String, +title: String, +look: Nat, +mood: String) -> Head: + match remote: + case True{}: + Head{title, look, mood, Mood.note(mood), ["chat"], "chat", False{}, False{}} + case False{}: + Head{title, look, mood, Mood.note(mood), BU.tabs.of(id), BU.tab.of(u, id), BU.viewer.of(id), BU.owns(id)} + +# the row a linked machine reports for a bot elsewhere (away, named by its +# id, while none does) +def BU.remote.find(rs: List<&2, BRow>, +id: String) -> BRow: + match rs: + case Nil{}: + BRow{id, id, "", 0n, "away", Mood.note("away"), True{}, True{}, "", ""} + case BRow{+i, +n, +th, +lk, +mo, +no, +se, +re, +pe, +l} <> t: + Bool.pick(BRow, String.eq(i, id), BRow{i, n, th, lk, mo, no, se, re, pe, l}, BU.remote.find(t, id)) + +def BU.remote.row(+u: U.Ui, +id: String) -> BRow: + BU.remote.find(BU.remote(u), id) + +def BRow.look(r: BRow) -> Nat: + match r: + case BRow{_, _, _, lk, _, _, _, _, _, _}: + lk + +def BRow.mood(r: BRow) -> String: + match r: + case BRow{_, _, _, _, m, _, _, _, _, _}: + m + +def BU.head.remote(+r: BRow, +u: U.Ui, +id: String) -> Head: + BU.head.m(True{}, u, id, BU.row.title(r), BRow.look(r), BRow.mood(r)) + +def BU.head.bot(+u: U.Ui, b: BT.Bot) -> Head: + match b: + case BT.Bot{+i, +n, +th, +lk, +pe, +c, +d}: + BU.head.m(False{}, u, i, U.Ui.far.title(u, i, n), lk, BU.mood(u, BT.Bot{i, n, th, lk, pe, c, d})) + +# what a bot's view shows above its tab (a room or the thread: nothing) +def BU.head(+u: U.Ui, v: View) -> Head: + match v: + case VBot{b}: + BU.head.bot(u, b) + case VRemote{+id}: + BU.head.remote(BU.remote.row(u, id), u, id) + case _: + Head{"", 0n, "away", Mood.note("away"), Nil{}, "chat", False{}, False{}} diff --git a/src/core/client.bend b/src/core/client.bend index 66947ada55..df25e6ceb0 100644 --- a/src/core/client.bend +++ b/src/core/client.bend @@ -13,6 +13,7 @@ import ./voice.bend as VC import ./space.bend as SPC import ./refs.bend as RF import ./mech.bend as MC +import ./far.bend as FR import ./desk.bend as DK import ./hist.bend as HI import ./links.bend as LK @@ -65,6 +66,11 @@ type Cmd is Data: type Act is Data: Act{ui: Ui, cmds: List<&2, Cmd>} +# The machine marker for projects, sidebar rows and timeline headers. +def Label.title(+st: M.State, +id: String, title: String) -> String: + FR.Fr.title(st, id, title) + + def Act.cmds(a: Act) -> List<&2, Cmd>: match a: case Act{_, cs}: @@ -210,7 +216,7 @@ def Local.keep.go(ks: List<&2, String>, +old: Map<&2, String>, m: Map<&2, String # (and the clock this client last ticked, which a folded turn's "5m ago" # reads) def Local.keep(+old: Map<&2, String>, m: Map<&2, String>) -> Map<&2, String>: - Local.keep.go(["local.machine", "local.machines", "local.drafts", "clock", "local.fixed"], old, m) + Local.keep.go(["cid", "local.machine", "local.machines", "local.drafts", "clock", "local.fixed", "local.generation", "local.state.hub", "local.state.gen"], old, m) def Machines.home.k(home: Bool, +nf: Map<&2, String>) -> Map<&2, String>: Bool.pick(Map<&2, String>, Bool.and(home, Bool.not(String.is_empty(Str.get(nf, "machines")))), Map.set(&2, String, nf, "local.machines", Str.get(nf, "machines")), nf) @@ -248,12 +254,30 @@ def Machines.current(rs: List<&2, MRow>) -> String: case MRow{+n, _, +on} <> t: Bool.pick(String, on, n, Machines.current(t)) -# switch to a machine: the host connects there, and its log replaces this -# one (the selection and anything still unsent belong to the old machine) +def Machine.draft(scratch: Bool, +k: String, dr: Map<&2, String>, out: Map<&2, String>) -> Map<&2, String>: + match scratch: + case True{}: + out + case False{}: + Map.set(&2, String, out, k, Str.get(dr, k)) + +# A transition discards every active scratch selector, live delta, viewer, +# terminal and outbox from the departing hub. Unsent thread drafts stay. +def Machine.drafts(ks: List<&2, String>, +dr: Map<&2, String>, out: Map<&2, String>) -> Map<&2, String>: + match ks: + case Nil{}: + out + case +k <> t: + Machine.drafts(t, dr, Machine.draft(String.starts_with(k, "@"), k, dr, out)) + +# The window assigns the actual connection tag when it opens the link. +# The transition itself also advances the generation, before any message +# can try to satisfy a selection against the old read model. def Ui.machine(u: Ui, +url: String) -> Act: match u: - case Ui{st, _, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, _, tr}: - Act{Ui{st, "", dr, live, Map.set(&2, Bool, op, "machines", False{}), on, now, ad, pa, v, rid, w, er, note, Map.set(&2, String, nf, "local.machine", url), Map.new(&2, String), tr}, [Connect{url}]} + case Ui{_, _, +dr, _, _, _, now, ad, _, _, rid, w, _, _, +nf, _, _}: + Act{Ui{M.State.empty(), "", Machine.drafts(Map.keys(&2, String, dr), dr, Map.new(&2, String)), Map.new(&2, String), Map.new(&2, Bool), False{}, now, ad, False{}, "", rid, w, "", "connecting", + Map.set(&2, String, Map.set(&2, String, Local.keep(nf, Map.new(&2, String)), "local.machine", url), "local.generation", "pending:" ++ Str.get(nf, "local.generation")), Map.new(&2, String), Map.new(&2, VT.Vt)}, [Connect{url}]} def Ui.info_merge(u: Ui, o: J.Json) -> Ui: match u o: @@ -282,7 +306,7 @@ def Ui.reset(u: Ui, +o: J.Json) -> Ui: # how many changes this client holds (a reconnect asks for the rest) # the log's read model handed over whole (the hub's own window: nothing # to parse or replay) -def Ui.adopt(u: Ui, st: M.State) -> Ui: +def Ui.adopt.raw(u: Ui, st: M.State) -> Ui: match u: case Ui{_, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr}: Ui{st, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr} @@ -571,7 +595,7 @@ def Ui.on_thread(u: Ui, m: String) -> Act: def Ui.select.k(u: Ui, +id: String) -> Ui: match u: case Ui{st, _, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr}: - Ui{st, id, Map.set(&2, String, Map.set(&2, String, Map.set(&2, String, Map.set(&2, String, Map.set(&2, String, dr, "@del", ""), "@bot", ""), "@room", ""), "@remote", ""), "@bot.want", ""), live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr} + Ui{st, id, Map.set(&2, String, Map.set(&2, String, Map.set(&2, String, Map.set(&2, String, Map.set(&2, String, Map.set(&2, String, dr, "@del", ""), "@bot", ""), "@room", ""), "@remote", ""), "@bot.want", ""), "@thread.want", ""), live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr} # Drafts kept # ----------- @@ -581,7 +605,7 @@ def Ui.select.k(u: Ui, +id: String) -> Ui: def Draft.safe.c(+c: U32) -> Bool: Bool.or(Bool.or(Bool.and(U32.is_ge(c, 48), U32.is_le(c, 57)), Bool.and(U32.is_ge(c, 97), U32.is_le(c, 122))), - Bool.or(Bool.and(U32.is_ge(c, 65), U32.is_le(c, 90)), Bool.or(U32.is_eq(c, 45), U32.is_eq(c, 95)))) + Bool.or(Bool.and(U32.is_ge(c, 65), U32.is_le(c, 90)), Bool.or(U32.is_eq(c, 126), Bool.or(U32.is_eq(c, 45), U32.is_eq(c, 95))))) def Draft.safe.go(s: String) -> Bool: match s: @@ -590,7 +614,7 @@ def Draft.safe.go(s: String) -> Bool: case SCon{Chr{c}, t}: Bool.and(Draft.safe.c(c), Draft.safe.go(t)) -# a name made only of letters, digits, '-' and '_' +# a name made only of letters, digits, '-', '_' and far's '~' separator def Draft.safe(s: String) -> Bool: match s: case SNil{}: @@ -3945,7 +3969,7 @@ def Bots.sel.set(u: Ui, +th: String) -> Ui: # (a bot still awaited, "@bot.want", gives way to what was chosen since) def Bots.views(u: Ui, +bot: String, +room: String, +remote: String) -> Ui: - Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(u, "@bot", bot), "@room", room), "@remote", remote), "@del", ""), "@bot.want", "") + Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(u, "@bot", bot), "@room", room), "@remote", remote), "@del", ""), "@bot.want", ""), "@thread.want", "") # a bot shown: its thread is selected, its tab kept def Bots.select.k(m: Maybe<&2, BT.Bot>, u: Ui) -> Act: @@ -4015,25 +4039,6 @@ def Far.machine(+j: J.Json, +pname: String, +purl: String) -> Maybe<&2, String>: def Far.chat(u: Ui, +id: String) -> Act: Act{Bots.views(u, "", "", id), [Focus{"remote-send"}, Scroll{}]} -def Far.opened(a: Act, +name: String) -> Act: - match a: - case Act{u, cs}: - Act{Ui.scratch_set(u, "@bot.want", name), cs} - -def Far.open(same: Bool, +u: Ui, +url: String, +id: String, +name: String) -> Act: - match same: - case True{}: - Far.chat(u, id) - case False{}: - Far.opened(Ui.machine(u, url), name) - -def Far.go(m: Maybe<&2, String>, +u: Ui, +id: String, +name: String) -> Act: - match m: - case Some{+url}: - Far.open(Bool.or(String.eq(url, Ui.info(u, "local.machine")), String.eq(Ui.info(u, "local.fixed"), "1")), u, url, id, name) - case None{}: - Far.chat(u, id) - # "name\u001ftext": a form field def Bots.bfield.k(ps: List<&2, String>, u: Ui) -> Ui: match ps: @@ -4082,17 +4087,28 @@ def Bots.peers.k(+ps: List<&2, BT.Peer>, +u: Ui) -> List<&2, BT.Peer>: def Bots.peers(+u: Ui) -> List<&2, BT.Peer>: Bots.peers.k(BT.World.peers(M.State.bots(Ui.state.of(u))), u) -# a bot elsewhere ("name@machine", BU.far's id): the machine it names, as -# BT.Peer.of finds it (an id first, then a name) -def Far.peer(m: Maybe<&2, BT.Peer>, +u: Ui, +id: String) -> Act: +def Far.mirrored(bs: List<&2, BT.Bot>, +key: String) -> Maybe<&2, BT.Bot>: + match bs: + case Nil{}: + None{} + case +b <> t: + Bool.pick(Maybe<&2, BT.Bot>, Bool.and(FR.Fr.is(BT.Bot.id(b)), String.eq(BT.Member.far(BT.Bot.name(b), FR.Fr.link(BT.Bot.id(b))), key)), Some{b}, Far.mirrored(t, key)) + +def Far.pick(m: Maybe<&2, BT.Bot>, +u: Ui, +id: String) -> Act: match m: - case Some{BT.Peer{_, +n, +url, _, _}}: - Far.go(Far.machine(Maybe.default(&2, J.Json, J.Json.parse(Ui.info(u, "local.machines")), J.Null{}), n, url), u, id, BT.Member.name(id)) + case Some{b}: + Bots.select(u, BT.Bot.id(b)) case None{}: Far.chat(u, id) +# a bot elsewhere, chosen: the bot itself when its machine shares its +# threads with this one (it is mirrored here: the same view as a bot +# here, at once, law far_pick_mirrored), else the conversation with it. +# The window never switches hubs to show a bot: that fetched the other +# hub's whole log (11-20 s on a 13 MB log). def Bots.remote.select(+u: Ui, +id: String) -> Act: - Far.peer(BT.Peer.of(Bots.peers(u), BT.Member.machine(id)), u, id) + Far.pick(Far.mirrored(BT.World.bots(M.State.bots(Ui.state.of(u))), BT.Member.key(Bots.peers(u), id)), u, id) + # the members that are not the bot k stands for (BT.Member.key: "Kit@box" # and "Kit@" are one bot) @@ -5038,13 +5054,68 @@ def Voice.flag.k(ask: Bool, a: Act) -> Act: def Voice.flag(+value: String, a: Act) -> Act: Voice.flag.k(String.eq(value, "settings"), a) -# the reply may beat the bot's own change: remember it, and show it when it comes +def Bots.reply.put(+o: J.Json, +key: String, +into: String, +u: Ui) -> Ui: + Bool.pick(Ui, String.is_empty(J.Json.get_str(o, key, "")), u, Ui.scratch_set(u, into, J.Json.get_str(o, key, ""))) + +# a key the answer has, even when empty (no account left is news) +def Bots.reply.has(m: Maybe<&2, J.Json>, +into: String, +u: Ui) -> Ui: + match m: + case Some{J.Str{s}}: + Ui.scratch_set(u, into, s) + case _: + u + +# A pending selection names both its hub and its connection generation. +# Only a read model installed for that exact pair can resolve it. +def Want.accept(hub: Bool, generation: Bool, installed: Bool) -> Bool: + match hub generation installed: + case True{} True{} True{}: + True{} + case _ _ _: + False{} + +def Want.bound(+u: Ui) -> Bool: + Want.accept(String.eq(Ui.scratch(u, "@want.hub"), Ui.info(u, "local.machine")), + String.eq(Ui.scratch(u, "@want.gen"), Ui.info(u, "local.generation")), + Bool.and(String.eq(Ui.info(u, "local.state.hub"), Ui.info(u, "local.machine")), String.eq(Ui.info(u, "local.state.gen"), Ui.info(u, "local.generation")))) + +def Want.bot(+u: Ui, name: String) -> Ui: + Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(u, "@thread.want", ""), "@bot.want", name), "@want.hub", Ui.info(u, "local.machine")), "@want.gen", Ui.info(u, "local.generation")) + +def Far.opened(a: Act, +name: String) -> Act: + match a: + case Act{u, cs}: + Act{Want.bot(u, name), cs} + +def Far.open(same: Bool, +u: Ui, +url: String, +id: String, +name: String) -> Act: + match same: + case True{}: + Far.chat(u, id) + case False{}: + Far.opened(Ui.machine(u, url), name) + +def Far.go(m: Maybe<&2, String>, +u: Ui, +id: String, +name: String) -> Act: + match m: + case Some{+url}: + Far.open(Bool.or(String.eq(url, Ui.info(u, "local.machine")), String.eq(Ui.info(u, "local.fixed"), "1")), u, url, id, name) + case None{}: + Far.chat(u, id) + +# a bot elsewhere ("name@machine", BU.far's id): the machine it names, as +# BT.Peer.of finds it (an id first, then a name) +def Far.peer(m: Maybe<&2, BT.Peer>, +u: Ui, +id: String) -> Act: + match m: + case Some{BT.Peer{_, +n, +url, _, _}}: + Far.go(Far.machine(Maybe.default(&2, J.Json, J.Json.parse(Ui.info(u, "local.machines")), J.Null{}), n, url), u, id, BT.Member.name(id)) + case None{}: + Far.chat(u, id) + def Bots.reply.show(m: Maybe<&2, BT.Bot>, +id: String, u: Ui) -> Ui: match m: case Some{BT.Bot{+i, _, +th, _, _, _, _}}: Bots.views(Bots.sel.set(u, th), i, "", "") case None{}: - Ui.scratch_set(u, "@bot.want", id) + Want.bot(u, id) def Bots.reply.bot.k(none: Bool, +o: J.Json, +u: Ui) -> Ui: match none: @@ -5058,17 +5129,6 @@ def Bots.reply.bot.k(none: Bool, +o: J.Json, +u: Ui) -> Ui: def Bots.reply.bot(+o: J.Json, u: Ui) -> Ui: Bots.reply.bot.k(String.is_empty(J.Json.get_str(o, "bot", "")), o, u) -def Bots.reply.put(+o: J.Json, +key: String, +into: String, +u: Ui) -> Ui: - Bool.pick(Ui, String.is_empty(J.Json.get_str(o, key, "")), u, Ui.scratch_set(u, into, J.Json.get_str(o, key, ""))) - -# a key the answer has, even when empty (no account left is news) -def Bots.reply.has(m: Maybe<&2, J.Json>, +into: String, +u: Ui) -> Ui: - match m: - case Some{J.Str{s}}: - Ui.scratch_set(u, into, s) - case _: - u - def Bots.reply(+o: J.Json, +u: Ui) -> Ui: Voice.reply(o, Bots.reply.has(J.Json.get(o, "googleAccounts"), "@google.accts", Bots.reply.put(o, "googleUrl", "@google.url", Bots.reply.put(o, "google", "@google", Bots.reply.put(o, "invite", "@peer.invite", Bots.reply.put(o, "secret", "@hook.secret", Bots.reply.put(o, "hook", "@hook.id", @@ -5078,6 +5138,55 @@ def Bots.reply(+o: J.Json, +u: Ui) -> Ui: def Ui.reply(u: Ui, +o: J.Json) -> Ui: Bots.reply(o, Proj.reply(o, Btw.reply(Ui.reply.skills(u, o), o))) +# A link's threads and bots (far.bend): its batch folded into the read +# model under this hub's ids, and whether it answered ("far.away.") +# and what it is called ("far.name.") kept for the rows that show it +def Far.arrived.m(m: Maybe<&2, BT.Bot>, u: Ui) -> Ui: + match m: + case Some{BT.Bot{+id, _, +th, _, _, _, _}}: + Ui.flag_off(Bots.views(Bots.sel.set(u, th), id, "", ""), "settings") + case None{}: + u + +def Far.arrived.k(waiting: Bool, +u: Ui) -> Ui: + match waiting: + case True{}: + Far.arrived.m(Far.mirrored(BT.World.bots(M.State.bots(Ui.state(u))), BT.Member.key(Bots.peers(u), Ui.scratch(u, "@remote"))), u) + case False{}: + u + +# A reported-only bot opens immediately. If its owner subsequently +# shares its thread, the same choice gains the normal timeline and tabs. +# Keep an unfinished remote composer in place until it has been sent. +def Far.arrived(+u: Ui) -> Ui: + Far.arrived.k(Bool.and(Bool.not(String.is_empty(Ui.scratch(u, "@remote"))), String.is_empty(Bots.field(u, "rtext"))), u) + +def Ui.far.st(u: Ui, st: M.State) -> Ui: + match u: + case Ui{_, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr}: + Ui{st, sel, dr, live, op, on, now, ad, pa, v, rid, w, er, note, nf, ob, tr} + +def Ui.far.k(+u: Ui, +link: String, +o: J.Json) -> Ui: + Ui.info_set(Ui.info_set(Ui.far.st(u, FR.Fr.mark(FR.Fr.recv(Ui.state(u), link, J.Json.get_str(o, "epoch", ""), J.Json.get_nat(o, "since", 0n), J.Json.get_nat(o, "head", 0n), + FR.Fr.items.of(J.Json.elems(J.Json.get_obj(o, "items")))), link, J.Json.get_str(o, "name", link), J.Json.get_bool(o, "away", False{}))), + "far.away." ++ link, Bool.pick(String, J.Json.get_bool(o, "away", False{}), "1", "")), "far.name." ++ link, J.Json.get_str(o, "name", link)) + +def Ui.far(+u: Ui, +o: J.Json) -> Ui: + Far.arrived(Ui.far.k(u, J.Json.get_str(o, "link", ""), o)) + +# whether the machine a thing elsewhere lives on did not answer last time +def Ui.far.away(+u: Ui, +id: String) -> Bool: + Bool.and(FR.Fr.is(id), String.eq(Ui.info(u, "far.away." ++ FR.Fr.link(id)), "1")) + +# the name of the machine a thing elsewhere lives on ("" for one here) +def Ui.far.name(+u: Ui, +id: String) -> String: + Bool.pick(String, FR.Fr.is(id), Bool.pick(String, String.is_empty(Ui.info(u, "far.name." ++ FR.Fr.link(id))), FR.Fr.link(id), Ui.info(u, "far.name." ++ FR.Fr.link(id))), "") + +# a thread's or bot's name as shown: "Kit · desk" for one elsewhere, with +# " (away)" while its machine does not answer +def Ui.far.title(+u: Ui, +id: String, +title: String) -> String: + Label.title(Ui.state(u), id, title) + def Ui.recv.k(i: Nat, u: Ui, +o: J.Json) -> Ui: match i: case 0n: @@ -5098,31 +5207,211 @@ def Ui.recv.k(i: Nat, u: Ui, +o: J.Json) -> Ui: Ui.info_merge(u, J.Json.get_obj(o, "info")) case 8n: Voice.recv(u, o) + case 9n: + Ui.far(u, o) case _: u +# A thread request uses the same binding as a bot request. +def Want.thread(+u: Ui, id: String) -> Ui: + Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(Ui.scratch_set(u, "@bot.want", ""), "@thread.want", id), "@want.hub", Ui.info(u, "local.machine")), "@want.gen", Ui.info(u, "local.generation")) + +def Want.rebind(bound: Bool, u: Ui, +generation: String) -> Ui: + match bound: + case True{}: + Ui.scratch_set(u, "@want.gen", generation) + case False{}: + u + +# Called only for the destination chosen by Ui.machine. Old socket +# messages are rejected by the host's connection tag before Ui.recv. +def Ui.connection(+u: Ui, +generation: String) -> Ui: + Ui.online(Ui.info_set(Want.rebind(Bool.and(String.eq(Ui.scratch(u, "@want.hub"), Ui.info(u, "local.machine")), String.eq(Ui.scratch(u, "@want.gen"), Ui.info(u, "local.generation"))), u, generation), "local.generation", generation), False{}) + +def Ui.installed(+u: Ui) -> Ui: + Ui.info_set(Ui.info_set(u, "local.state.hub", Ui.info(u, "local.machine")), "local.state.gen", Ui.info(u, "local.generation")) + def Bots.wanted.m(m: Maybe<&2, BT.Bot>, u: Ui) -> Ui: match m: case Some{BT.Bot{+i, _, +th, _, _, _, _}}: - Ui.scratch_set(Bots.views(Bots.sel.set(u, th), i, "", ""), "@bot.want", "") + Bots.views(Bots.sel.set(u, th), i, "", "") case None{}: u -def Bots.wanted.k(none: Bool, +u: Ui) -> Ui: - match none: +def Want.thread.k(found: Bool, u: Ui, id: String) -> Ui: + match found: case True{}: + Ui.scratch_set(Bots.views(Bots.sel.set(u, id), "", "", ""), "@thread.want", "") + case False{}: u + +def Want.thread.has(r: Map<&2, M.Thread> & Bool) -> Bool: + (_, found) = r + found + +def Want.thread.exists(st: M.State, +id: String) -> Bool: + match st: + case M.State{_, ts, _, _, _, _, _, _, _, _, _}: + Want.thread.has(Map.has(&2, M.Thread, ts, id)) + +def Bots.wanted.k(bound: Bool, +u: Ui) -> Ui: + match bound: + case True{}: + Want.thread.k(Bool.and(Bool.not(String.is_empty(Ui.scratch(u, "@thread.want"))), Want.thread.exists(Ui.state.of(u), Ui.scratch(u, "@thread.want"))), + Bots.wanted.m(BT.World.find(M.State.bots(Ui.state.of(u)), Ui.scratch(u, "@bot.want")), u), Ui.scratch(u, "@thread.want")) case False{}: - Bots.wanted.m(BT.World.find(M.State.bots(Ui.state.of(u)), Ui.scratch(u, "@bot.want")), u) + u + +def Bots.wanted.any(pending: Bool, +u: Ui) -> Ui: + match pending: + case True{}: + Bots.wanted.k(Want.bound(u), u) + case False{}: + u -# a bot asked for (just made, or opened on another machine: an id or a -# name) is shown once its change has come def Bots.wanted(+u: Ui) -> Ui: - Bots.wanted.k(String.is_empty(Ui.scratch(u, "@bot.want")), u) + Bots.wanted.any(Bool.or(Bool.not(String.is_empty(Ui.scratch(u, "@bot.want"))), Bool.not(String.is_empty(Ui.scratch(u, "@thread.want")))), u) + +# Adoption installs the complete state before resolving a pending want +# and restoring that thread's viewer. No empty-log intermediate selection. +def Ui.adopt(+u: Ui, st: M.State) -> Ui: + Ui.reselect(Ui.sel(u), Bots.wanted(Ui.installed(Ui.adopt.raw(u, st)))) -# a message from the server +def Ui.adopt_log(u: Ui, st: M.State, o: J.Json) -> Ui: + Ui.adopt(Ui.reset(u, o), st) + +def Ui.join(log: Bool, u: Ui) -> Ui: + match log: + case True{}: + Ui.installed(u) + case False{}: + u + +# A message can resolve a want only against the installed destination. def Ui.recv(+u: Ui, +o: J.Json) -> Ui: - Ui.reselect(Ui.sel(u), Bots.wanted(Ui.recv.k(C.Codec.pos(["log", "changes", "delta", "reply", "update", "view", "term", "info", "voice"], J.Json.get_str(o, "t", "")), u, o))) + Ui.reselect(Ui.sel(u), Bots.wanted(Ui.join(String.eq(J.Json.get_str(o, "t", ""), "log"), Ui.recv.k(C.Codec.pos(["log", "changes", "delta", "reply", "update", "view", "term", "info", "voice", "far"], J.Json.get_str(o, "t", "")), u, o)))) + +# The cache contains home and at most one linked hub; each slot keeps its +# read model and machine info (including the log origin) together. +type HubView is Data: + HubView{hub: String, state: M.State, info: Map<&2, String>} + +def HubView.hub(v: HubView) -> String: + match v: + case HubView{hub, _, _}: + hub + +def HubCache.home.k(home: Bool, v: HubView, rest: List<&2, HubView>) -> List<&2, HubView>: + match home: + case True{}: + [v] + case False{}: + rest + +def HubCache.home(vs: List<&2, HubView>) -> List<&2, HubView>: + match vs: + case Nil{}: + Nil{} + case +v <> t: + HubCache.home.k(String.is_empty(HubView.hub(v)), v, HubCache.home(t)) + +def HubCache.remote.k(home: Bool, v: HubView, rest: List<&2, HubView>) -> List<&2, HubView>: + match home: + case True{}: + rest + case False{}: + [v] + +def HubCache.remote(vs: List<&2, HubView>) -> List<&2, HubView>: + match vs: + case Nil{}: + Nil{} + case +v <> t: + HubCache.remote.k(String.is_empty(HubView.hub(v)), v, HubCache.remote(t)) + +def HubCache.put.k(home: Bool, v: HubView, vs: List<&2, HubView>) -> List<&2, HubView>: + match home: + case True{}: + v <> HubCache.remote(vs) + case False{}: + v <> HubCache.home(vs) + +def HubCache.put(+v: HubView, vs: List<&2, HubView>) -> List<&2, HubView>: + HubCache.put.k(String.is_empty(HubView.hub(v)), v, vs) + +def HubCache.get.k(same: Bool, v: HubView, rest: Maybe<&2, HubView>) -> Maybe<&2, HubView>: + match same: + case True{}: + Some{v} + case False{}: + rest + +def HubCache.get(vs: List<&2, HubView>, +hub: String) -> Maybe<&2, HubView>: + match vs: + case Nil{}: + None{} + case +v <> t: + HubCache.get.k(String.eq(HubView.hub(v), hub), v, HubCache.get(t, hub)) + +def Ui.infos(u: Ui) -> Map<&2, String>: + match u: + case Ui{_, _, _, _, _, _, _, _, _, _, _, _, _, _, nf, _, _}: + nf + +def HubCache.save.k(installed: Bool, +u: Ui, vs: List<&2, HubView>) -> List<&2, HubView>: + match installed: + case True{}: + HubCache.put(HubView{Ui.info(u, "local.machine"), Ui.state.of(u), Ui.infos(u)}, vs) + case False{}: + vs + +def HubCache.save(+u: Ui, vs: List<&2, HubView>) -> List<&2, HubView>: + HubCache.save.k(Bool.and(String.eq(Ui.info(u, "local.machine"), Ui.info(u, "local.state.hub")), String.eq(Ui.info(u, "local.generation"), Ui.info(u, "local.state.gen"))), u, vs) + +def Ui.cached.raw(u: Ui, v: HubView) -> Ui: + match u v: + case Ui{_, sel, dr, live, op, on, now, ad, pa, ver, rid, w, er, note, +nf, ob, tr} HubView{_, st, info}: + Ui{st, sel, dr, live, op, on, now, ad, pa, ver, rid, w, er, note, Local.keep(nf, info), ob, tr} + +def Ui.cached.k(same: Bool, +u: Ui, v: HubView) -> Ui: + match same: + case True{}: + Ui.reselect(Ui.sel(u), Bots.wanted(Ui.installed(Ui.cached.raw(u, v)))) + case False{}: + u + +def Ui.cached(m: Maybe<&2, HubView>, +u: Ui) -> Ui: + match m: + case Some{+v}: + Ui.cached.k(String.eq(HubView.hub(v), Ui.info(u, "local.machine")), u, v) + case None{}: + u + +# Whether the actual main area uses the thread timeline. Rooms and remote +# conversations have their own posts, and every bot tab except Chat has +# its own pane. Keep this decision before any wrapping work. +def Timeline.visible(settings: Bool, selected: Bool, thread: Bool) -> Bool: + match settings selected thread: + case False{} True{} True{}: + True{} + case _ _ _: + False{} + +def Timeline.bot(m: Maybe<&2, BT.Bot>, +u: Ui) -> Bool: + match m: + case Some{BT.Bot{_, _, +th, _, _, _, _}}: + Bool.or(Bool.not(String.eq(th, Ui.sel(u))), Bool.or(String.is_empty(Ui.scratch(u, "@btab")), String.eq(Ui.scratch(u, "@btab"), "chat"))) + case None{}: + True{} + +def Timeline.room(r: BT.Room, +u: Ui) -> Bool: + match r: + case BT.Room{_, _, _, _, deleted}: + Bool.and(deleted, Bool.and(String.is_empty(Ui.scratch(u, "@remote")), Timeline.bot(BT.World.find.id(BT.World.bot(M.State.bots(Ui.state.of(u)), Ui.scratch(u, "@bot"))), u))) + +def Ui.timeline(+u: Ui) -> Bool: + Timeline.visible(Ui.flag(u, "settings"), Bool.not(String.is_empty(Ui.sel(u))), Timeline.room(BT.World.room(M.State.bots(Ui.state.of(u)), Ui.scratch(u, "@room")), u)) + def Ui.shows.k(delta: Bool, +sel: String, +o: J.Json) -> Bool: match delta: diff --git a/src/core/far.bend b/src/core/far.bend new file mode 100644 index 0000000000..6538c2f953 --- /dev/null +++ b/src/core/far.bend @@ -0,0 +1,1126 @@ +import Base +import ./json.bend as J +import ./model.bend as M +import ./codec.bend as C +import ./bot.bend as BT + +# Threads that live on a linked machine (docs/bots.md, "Threads elsewhere"). +# +# A hub shares its bots and their threads with the owner's other machines +# it is linked to. The owning hub stays the authority: it pushes each +# shared change, as its own log numbers it, to every such peer (POST +# /far/push), and a peer that missed some asks for them (POST /far/log, +# paged). The peer keeps what it got (its mirror, one per link) and hands +# it to its clients, which fold it into their read model under ids of +# their own: a thing that lives elsewhere is named by the link and its +# id there ("~", Fr.id), so nothing here can be mistaken for it +# and nothing shows twice. What a client does to such a thread (write, +# stop, answer) goes back to its hub (Fr.route, POST /far/rpc). +# +# Order and loss: a batch says which log number it follows (`since`) and +# where it ends (`to`). A mirror takes a batch only when it follows what it +# has (since <= head), and only the items it lacks (n > head); a batch past +# a gap changes nothing and asks for what is missing (Fr.take). So a +# batch that comes twice, late, or out of order is harmless, and a restart +# or a partition on either side ends in one pull. + +# Ids +# --- + +# a thing on the machine a link reaches, named here +def Fr.id(+link: String, +id: String) -> String: + link ++ "~" ++ id + +# the same, keeping "" (no project, no parent) as it is +def Fr.opt(+link: String, +id: String) -> String: + Bool.pick(String, String.is_empty(id), "", Fr.id(link, id)) + +# whether an id names a thing elsewhere (local ids never hold a "~") +def Fr.is(+id: String) -> Bool: + String.contains(id, "~") + +def Fr.link.k(ps: List<&2, String>) -> String: + match ps: + case l <> _ <> _: + l + case _: + "" + +# the link an id elsewhere lives behind ("" for an id here) +def Fr.link(+id: String) -> String: + Fr.link.k(String.split(id, '~')) + +def Fr.local.k(ps: List<&2, String>, +id: String) -> String: + match ps: + case _ <> +t <> +r: + String.join(t <> r, "~") + case _: + id + +# its id on its own machine (an id here is itself) +def Fr.local(+id: String) -> String: + Fr.local.k(String.split(id, '~'), id) + +# Changes, as a client here reads them +# ------------------------------------ +# The owning hub's change renamed into this hub's ids. Only what shows a +# thread, a bot and its space goes; settings, devices, links, webhooks, +# rooms (shared rooms travel on their own), ordering and usage stay home +# (None). + +def Fr.change(+l: String, c: M.Change) -> Maybe<&2, M.Change>: + match c: + case M.ProjectCreated{+id, title, root, at}: + Some{M.ProjectCreated{Fr.id(l, id), title, root, at}} + case M.ProjectRemoved{+id, at}: + Some{M.ProjectRemoved{Fr.id(l, id), at}} + case M.ThreadCreated{+id, +project, title, env, branch, worktree, provider, model, at}: + Some{M.ThreadCreated{Fr.id(l, id), Fr.opt(l, project), title, env, branch, worktree, provider, model, at}} + case M.ThreadRenamed{+id, title, at}: + Some{M.ThreadRenamed{Fr.id(l, id), title, at}} + case M.MessagePosted{+thread, msg, role, text, at}: + Some{M.MessagePosted{Fr.id(l, thread), msg, role, text, at}} + case M.TurnChanged{+thread, state, at}: + Some{M.TurnChanged{Fr.id(l, thread), state, at}} + case M.PendingChanged{+thread, count, at}: + Some{M.PendingChanged{Fr.id(l, thread), count, at}} + case M.ActivityLogged{+thread, act, tone, kind, summary, at}: + Some{M.ActivityLogged{Fr.id(l, thread), act, tone, kind, summary, at}} + case M.ThreadSettled{+id, manual, at}: + Some{M.ThreadSettled{Fr.id(l, id), manual, at}} + case M.ThreadUnsettled{+id, at}: + Some{M.ThreadUnsettled{Fr.id(l, id), at}} + case M.ThreadArchived{+id, b, at}: + Some{M.ThreadArchived{Fr.id(l, id), b, at}} + case M.ThreadDeleted{+id, at}: + Some{M.ThreadDeleted{Fr.id(l, id), at}} + case M.ModesSet{+id, runtime, interaction, model, at}: + Some{M.ModesSet{Fr.id(l, id), runtime, interaction, model, at}} + case M.TaskDelegated{+task, +parent, +child, depth, provider, model, at}: + Some{M.TaskDelegated{Fr.id(l, task), Fr.opt(l, parent), Fr.opt(l, child), depth, provider, model, at}} + case M.TaskStateSet{+task, state, summary, at}: + Some{M.TaskStateSet{Fr.id(l, task), state, summary, at}} + case M.TaskDelivered{+task, at}: + Some{M.TaskDelivered{Fr.id(l, task), at}} + case M.TurnQueued{+thread, msg, text, at}: + Some{M.TurnQueued{Fr.id(l, thread), msg, text, at}} + case M.QueueTaken{+thread, at}: + Some{M.QueueTaken{Fr.id(l, thread), at}} + case M.AskOpened{+id, +thread, kind, tool, detail, options, at}: + Some{M.AskOpened{Fr.id(l, id), Fr.id(l, thread), kind, tool, detail, options, at}} + case M.AskClosed{+id, answer, at}: + Some{M.AskClosed{Fr.id(l, id), answer, at}} + case M.QueueEdited{+thread, msg, text, at}: + Some{M.QueueEdited{Fr.id(l, thread), msg, text, at}} + case M.QueueRemoved{+thread, msg, at}: + Some{M.QueueRemoved{Fr.id(l, thread), msg, at}} + case M.QueueMoved{+thread, msg, to, at}: + Some{M.QueueMoved{Fr.id(l, thread), msg, to, at}} + case M.QueueTakenOne{+thread, msg, at}: + Some{M.QueueTakenOne{Fr.id(l, thread), msg, at}} + case M.ProviderSet{+id, provider, model, at}: + Some{M.ProviderSet{Fr.id(l, id), provider, model, at}} + case M.TodosSet{+thread, todos, at}: + Some{M.TodosSet{Fr.id(l, thread), todos, at}} + case M.BotSet{+id, name, +thread, look, persona, at}: + Some{M.BotSet{Fr.id(l, id), name, Fr.id(l, thread), look, persona, at}} + case M.BotDeleted{+id, at}: + Some{M.BotDeleted{Fr.id(l, id), at}} + case M.RoutineSet{+id, +bot, name, cron, prompt, on, at}: + Some{M.RoutineSet{Fr.id(l, id), Fr.id(l, bot), name, cron, prompt, on, at}} + case M.RoutineDeleted{+id, at}: + Some{M.RoutineDeleted{Fr.id(l, id), at}} + case M.RoutineFired{+id, at}: + Some{M.RoutineFired{Fr.id(l, id), at}} + case M.MemorySaved{+bot, key, kind, text, tags, at}: + Some{M.MemorySaved{Fr.id(l, bot), key, kind, text, tags, at}} + case M.MemoryForgot{+bot, key, at}: + Some{M.MemoryForgot{Fr.id(l, bot), key, at}} + case M.SpaceSet{+bot, spec, at}: + Some{M.SpaceSet{Fr.id(l, bot), spec, at}} + case M.SpacePageSet{+bot, html, at}: + Some{M.SpacePageSet{Fr.id(l, bot), html, at}} + case _: + None{} + +# What the owning hub shares +# -------------------------- +# Its projects, bots and threads, shared only over an owner-trusted link +# by the server. Settings, devices, links, hooks and rooms stay home. + +def Fr.st.threads(s: M.State) -> Map<&2, M.Thread>: + match s: + case M.State{_, ts, _, _, _, _, _, _, _, _, _}: + ts + +def Fr.st.asks(s: M.State) -> Map<&2, M.Ask>: + match s: + case M.State{_, _, _, _, _, _, _, ak, _, _, _}: + ak + +def Fr.st.tasks(s: M.State) -> Map<&2, M.Task>: + match s: + case M.State{_, _, _, _, tk, _, _, _, _, _, _}: + tk + +def Fr.thread.project(t: M.Thread) -> String: + match t: + case M.Thread{_, p, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}: + p + +def Fr.got.thread(r: Map<&2, M.Thread> & M.Thread) -> M.Thread: + (_, t) = r + t + +# A thread owned here. Mirrors are client projections, never re-exported. +def Fr.has(r: Map<&2, M.Thread> & Bool) -> Bool: + (_, b) = r + b + +def Fr.thread.shared(+s: M.State, +th: String) -> Bool: + Bool.and(Fr.has(Map.has(&2, M.Thread, Fr.st.threads(s), th)), + Bool.not(Fr.is(th))) + +def Fr.ask.thread(r: Map<&2, M.Ask> & M.Ask) -> String: + match r: + case (_, M.Ask{_, t, _, _, _, _, _, _}): + t + +def Fr.task.parent(r: Map<&2, M.Task> & M.Task) -> String: + match r: + case (_, M.Task{_, p, _, _, _, _, _, _, _}): + p + +def Fr.ask.shared(+s: M.State, +id: String) -> Bool: + Fr.thread.shared(s, Fr.ask.thread(Map.get(M.Ask, M.Ask{id, "", "", "", "", "", False{}, ""}, Fr.st.asks(s), id))) + +def Fr.task.shared(+s: M.State, +id: String) -> Bool: + Fr.thread.shared(s, Fr.task.parent(Map.get(M.Task, M.Task.blank(id), Fr.st.tasks(s), id))) + +# whether a change (with the state after it) goes to the owner's other +# machines +def Fr.shared(+s: M.State, c: M.Change) -> Bool: + match c: + case M.ProjectCreated{+id, _, _, _}: + Bool.not(Fr.is(id)) + case M.ProjectRemoved{+id, _}: + Bool.not(Fr.is(id)) + case M.ThreadCreated{+id, _, _, _, _, _, _, _, _}: + Fr.thread.shared(s, id) + case M.ThreadRenamed{+id, _, _}: + Fr.thread.shared(s, id) + case M.MessagePosted{+thread, _, _, _, _}: + Fr.thread.shared(s, thread) + case M.TurnChanged{+thread, _, _}: + Fr.thread.shared(s, thread) + case M.PendingChanged{+thread, _, _}: + Fr.thread.shared(s, thread) + case M.ActivityLogged{+thread, _, _, _, _, _}: + Fr.thread.shared(s, thread) + case M.ThreadSettled{+id, _, _}: + Fr.thread.shared(s, id) + case M.ThreadUnsettled{+id, _}: + Fr.thread.shared(s, id) + case M.ThreadArchived{+id, _, _}: + Fr.thread.shared(s, id) + case M.ThreadDeleted{+id, _}: + Fr.thread.shared(s, id) + case M.ModesSet{+id, _, _, _, _}: + Fr.thread.shared(s, id) + case M.TaskDelegated{_, +parent, _, _, _, _, _}: + Fr.thread.shared(s, parent) + case M.TaskStateSet{+task, _, _, _}: + Fr.task.shared(s, task) + case M.TaskDelivered{+task, _}: + Fr.task.shared(s, task) + case M.TurnQueued{+thread, _, _, _}: + Fr.thread.shared(s, thread) + case M.QueueTaken{+thread, _}: + Fr.thread.shared(s, thread) + case M.AskOpened{_, +thread, _, _, _, _, _}: + Fr.thread.shared(s, thread) + case M.AskClosed{+id, _, _}: + Fr.ask.shared(s, id) + case M.QueueEdited{+thread, _, _, _}: + Fr.thread.shared(s, thread) + case M.QueueRemoved{+thread, _, _}: + Fr.thread.shared(s, thread) + case M.QueueMoved{+thread, _, _, _}: + Fr.thread.shared(s, thread) + case M.QueueTakenOne{+thread, _, _}: + Fr.thread.shared(s, thread) + case M.ProviderSet{+id, _, _, _}: + Fr.thread.shared(s, id) + case M.TodosSet{+thread, _, _}: + Fr.thread.shared(s, thread) + case M.BotSet{_, _, _, _, _, _}: + True{} + case M.BotDeleted{_, _}: + True{} + case M.RoutineSet{_, _, _, _, _, _, _}: + True{} + case M.RoutineDeleted{_, _}: + True{} + case M.RoutineFired{_, _}: + True{} + case M.MemorySaved{_, _, _, _, _, _}: + True{} + case M.MemoryForgot{_, _, _}: + True{} + case M.SpaceSet{_, _, _}: + True{} + case M.SpacePageSet{_, _, _}: + True{} + case _: + False{} + +# Batches +# ------- +# An item is a change as its own hub numbers it (its place in that log) +# and sends it (its wire JSON, ids its own). + +type Item is Data: + Item{n: Nat, c: J.Json} + +def Item.n(i: Item) -> Nat: + match i: + case Item{n, _}: + n + +def Item.json(i: Item) -> J.Json: + match i: + case Item{n, c}: + J.Json.obj([J.KV{"n", J.Json.nat(n)}, J.KV{"c", c}]) + +def Item.of(+o: J.Json) -> Item: + Item{J.Json.get_nat(o, "n", 0n), Maybe.default(&2, J.Json, J.Json.get(o, "c"), J.Null{})} + +def Fr.items.of(os: List<&2, J.Json>) -> List<&2, Item>: + match os: + case Nil{}: + Nil{} + case +o <> t: + Item.of(o) <> Fr.items.of(t) + +def Fr.items.json(xs: List<&2, Item>) -> List<&2, J.Json>: + match xs: + case Nil{}: + Nil{} + case x <> t: + Item.json(x) <> Fr.items.json(t) + +def Fr.fresh.k(keep: Bool, x: Item, rest: List<&2, Item>) -> List<&2, Item>: + match keep: + case True{}: + x <> rest + case False{}: + rest + +# the items after head, in order +def Fr.fresh(xs: List<&2, Item>, +head: Nat) -> List<&2, Item>: + match xs: + case Nil{}: + Nil{} + case Item{+n, c} <> t: + Fr.fresh.k(Nat.is_gt(n, head), Item{n, c}, Fr.fresh(t, head)) + +# what a batch leaves: the new head, the items to take, and whether a gap +# came first (then nothing is taken and the rest is asked for) +type Got is Data: + Got{head: Nat, items: List<&2, Item>, gap: Bool} + +def Fr.take.k(gap: Bool, +head: Nat, +to: Nat, xs: List<&2, Item>) -> Got: + match gap: + case True{}: + Got{head, Nil{}, True{}} + case False{}: + Got{Bool.pick(Nat, Nat.is_gt(to, head), to, head), Fr.fresh(xs, head), False{}} + +# a batch that follows `since` and ends at `to`, for a mirror at `head` +def Fr.take(+head: Nat, +since: Nat, +to: Nat, xs: List<&2, Item>) -> Got: + Fr.take.k(Nat.is_gt(since, head), head, to, xs) + +def Got.head(g: Got) -> Nat: + match g: + case Got{h, _, _}: + h + +def Got.items(g: Got) -> List<&2, Item>: + match g: + case Got{_, xs, _}: + xs + +def Got.gap(g: Got) -> Bool: + match g: + case Got{_, _, x}: + x + +# A client's fold +# --------------- +# A client keeps how far it has each link's changes beside the read model +# (a Nav under "far~", so a fresh log forgets both at once), folds +# in only what it lacks, and never counts those changes in its own log's +# sequence (M.State.apply.go: a reconnect still asks this hub for exactly +# the changes it missed). + +def Fr.nav(+link: String) -> String: + "far~" ++ link + +def Fr.head(+s: M.State, +link: String) -> Nat: + M.Nav.until(M.Nav.get(M.State.navs(s), Fr.nav(link))) + +def Fr.head.set(s: M.State, +link: String, +h: Nat) -> M.State: + M.State.nav((x => M.Nav.snoozed(x, h)), s, Fr.nav(link)) + +def Fr.apply.k(m: Maybe<&2, M.Change>, s: M.State) -> M.State: + match m: + case Some{c}: + M.State.apply.go(s, c) + case None{}: + s + +def Fr.apply.c(m: Maybe<&2, M.Change>, +link: String, s: M.State) -> M.State: + match m: + case Some{c}: + Fr.apply.k(Fr.change(link, c), s) + case None{}: + s + +def Fr.apply(xs: List<&2, Item>, +link: String, s: M.State) -> M.State: + match xs: + case Nil{}: + s + case Item{_, +c} <> t: + Fr.apply(t, link, Fr.apply.c(C.Change.decode(c), link, s)) + +def Fr.fold.k(g: Got, +link: String, s: M.State) -> M.State: + match g: + case Got{_, _, True{}}: + s + case Got{h, xs, False{}}: + Fr.head.set(Fr.apply(xs, link, s), link, h) + +# a link's batch folded into a client's read model +def Fr.fold(+s: M.State, +link: String, +since: Nat, +to: Nat, xs: List<&2, Item>) -> M.State: + Fr.fold.k(Fr.take(Fr.head(s, link), since, to, xs), link, s) + +# Machine metadata lives beside the cursor in the client projection, so +# every UI can label a row from M.State without a separate info lookup. +def Fr.mark(s: M.State, +link: String, +name: String, +away: Bool) -> M.State: + M.State.nav((x => M.Nav{M.Nav.until(x), name, Bool.pick(String, away, "away", "")}), s, Fr.nav(link)) + +def Fr.name(+s: M.State, +id: String) -> String: + M.Nav.key(M.Nav.get(M.State.navs(s), Fr.nav(Fr.link(id))), "pinned") + +def Fr.away(+s: M.State, +id: String) -> Bool: + Bool.and(Fr.is(id), String.eq(M.Nav.key(M.Nav.get(M.State.navs(s), Fr.nav(Fr.link(id))), "active"), "away")) + +def Fr.title.k(far: Bool, +s: M.State, +id: String, title: String) -> String: + match far: + case False{}: + title + case True{}: + title ++ " · " ++ Bool.pick(String, String.is_empty(Fr.name(s, id)), Fr.link(id), Fr.name(s, id)) ++ Bool.pick(String, Fr.away(s, id), " (away)", "") + +def Fr.title(+s: M.State, +id: String, title: String) -> String: + Fr.title.k(Fr.is(id), s, id, title) + +# A history reset removes only that link's projection; it never changes +# the local sequence, local entries, or another machine's mirror. +def Fr.map.step(-A: Data, remove: Bool, m: Map<&2, A>, key: String) -> Map<&2, A>: + match remove: + case True{}: + Map.del(&2, A, m, key) + case False{}: + m + +def Fr.map.go(-A: Data, ks: List<&2, String>, m: Map<&2, A>, +prefix: String) -> Map<&2, A>: + match ks: + case Nil{}: + m + case +k <> t: + Fr.map.go(A, t, Fr.map.step(A, String.starts_with(k, prefix), m, k), prefix) + +def Fr.map(-A: Data, +m: Map<&2, A>, +prefix: String) -> Map<&2, A>: + Fr.map.go(A, Map.keys(&2, A, m), m, prefix) + +def Fr.entries.keep(remove: Bool, e: M.Entry, acc: List<&2, M.Entry>) -> List<&2, M.Entry>: + match remove: + case True{}: + acc + case False{}: + e <> acc + +def Fr.entries.go(es: List<&2, M.Entry>, +prefix: String, acc: List<&2, M.Entry>) -> List<&2, M.Entry>: + match es: + case Nil{}: + List.reverse(&2, M.Entry, acc) + case +e <> t: + Fr.entries.go(t, prefix, Fr.entries.keep(String.starts_with(M.Idx.thread(e), prefix), e, acc)) + +def Fr.index(es: List<&2, M.Entry>, by: Map<&2, List<&2, M.Entry>>) -> Map<&2, List<&2, M.Entry>>: + match es: + case Nil{}: + by + case +e <> t: + Fr.index(t, M.Idx.post(by, e)) + +def Fr.world(w: BT.World, +prefix: String) -> BT.World: + match w: + case BT.World{bs, rs, ps, hs, ro, ms, pe, sp, ho, ta, pg}: + BT.World{Fr.map(BT.Bot, bs, prefix), rs, ps, hs, Fr.map(BT.Routine, ro, prefix), Fr.map(BT.Mem, ms, prefix), pe, + Fr.map(String, sp, prefix), Fr.map(BT.Hops, ho, prefix), Fr.map(Nat, ta, prefix), Fr.map(BT.Page, pg, prefix)} + +def Fr.ext(bw: List<&2, M.Ext>, +prefix: String, es: List<&2, M.Entry>) -> List<&2, M.Ext>: + [M.Ext{Fr.world(M.Ext.world(M.Ext.of(bw)), prefix), Fr.index(List.reverse(&2, M.Entry, es), Map.new(&2, List<&2, M.Entry>))}] + +def Fr.forget(s: M.State, +link: String) -> M.State: + match s: + case M.State{ps, ts, +es, n, tk, sg, qu, ak, dv, +nv, bw}: + +prefix = link ++ "~" + +kept = Fr.entries.go(es, prefix, Nil{}) + M.State{Fr.map(M.Project, ps, prefix), Fr.map(M.Thread, ts, prefix), kept, n, Fr.map(M.Task, tk, prefix), sg, + Fr.map(List<&2, M.QItem>, qu, prefix), Fr.map(M.Ask, ak, prefix), dv, + Map.del(&2, M.Nav, Fr.map(M.Nav, nv, prefix), Fr.nav(link)), Fr.ext(bw, prefix, kept)} + +def Fr.epoch.nav(+link: String) -> String: + "epoch~" ++ link + +def Fr.epoch.of(+s: M.State, +link: String) -> String: + M.Nav.key(M.Nav.get(M.State.navs(s), Fr.epoch.nav(link)), "pinned") + +def Fr.epoch.keep(s: M.State, +link: String, +epoch: String) -> M.State: + M.State.nav((x => M.Nav.keyed(x, "pinned", epoch)), s, Fr.epoch.nav(link)) + +def Fr.recv.base(reset: Bool, s: M.State, +link: String) -> M.State: + match reset: + case True{}: + Fr.forget(s, link) + case False{}: + s + +def Fr.epoch.changed(+old: String, +epoch: String) -> Bool: + Bool.and(Bool.not(String.is_empty(epoch)), Bool.not(String.eq(old, epoch))) + +def Fr.recv(+s: M.State, +link: String, +epoch: String, +since: Nat, +to: Nat, xs: List<&2, Item>) -> M.State: + Fr.epoch.keep(Fr.fold(Fr.recv.base(Fr.epoch.changed(Fr.epoch.of(s, link), epoch), s, link), link, since, to, xs), link, + Bool.pick(String, String.is_empty(epoch), Fr.epoch.of(s, link), epoch)) + +# A hub's mirror of a link +# ------------------------ +# What the other end sent, in its order, how far it goes, its name, and +# whether it answered last time (away: shown, stale, until it does). + +type In is Data: + In{link: String, name: String, head: Nat, items: List<&2, Item>, away: Bool, epoch: String} + +def In.new(+link: String, +name: String) -> In: + In{link, name, 0n, Nil{}, True{}, ""} + +def In.link(i: In) -> String: + match i: + case In{l, _, _, _, _, _}: + l + +def In.head(i: In) -> Nat: + match i: + case In{_, _, h, _, _, _}: + h + +def In.away(i: In) -> Bool: + match i: + case In{_, _, _, _, a, _}: + a + +def In.away.set(i: In, a: Bool) -> In: + match i: + case In{l, n, h, xs, _, e}: + In{l, n, h, xs, a, e} + +def In.name.set(i: In, +nm: String) -> In: + match i: + case In{l, n, h, xs, a, e}: + In{l, Bool.pick(String, String.is_empty(nm), n, nm), h, xs, a, e} + +def In.put.k(g: Got, i: In) -> In: + match g i: + case Got{_, _, True{}} _: + i + case Got{h, ys, False{}} In{l, n, _, xs, _, e}: + In{l, n, h, List.append(&2, Item, xs, ys), False{}, e} + +# a batch into the mirror (it answered, so it is here) +def In.put(+i: In, +since: Nat, +to: Nat, xs: List<&2, Item>) -> In: + In.put.k(Fr.take(In.head(i), since, to, xs), i) + +# what a batch brings that the mirror lacks (Nil on a gap) +def In.got(+i: In, +since: Nat, +to: Nat, xs: List<&2, Item>) -> Got: + Fr.take(In.head(i), since, to, xs) + +# the message a client folds: {"t":"far","link","name","away","since","head","items"} +def Fr.msg(+link: String, +name: String, away: Bool, +since: Nat, +head: Nat, xs: List<&2, Item>) -> String: + J.Json.show(J.Json.obj([J.KV{"t", J.Str{"far"}}, J.KV{"link", J.Str{link}}, J.KV{"name", J.Str{name}}, J.KV{"away", J.Json.bool(away)}, + J.KV{"since", J.Json.nat(since)}, J.KV{"head", J.Json.nat(head)}, J.KV{"items", J.Json.arr(Fr.items.json(xs))}])) + +def Fr.epoch.fields(o: J.Json) -> J.Json: + match o: + case J.Obj{fs}: + fs + case _: + J.End{} + +def In.epoch(i: In) -> String: + match i: + case In{_, _, _, _, _, e}: + e + +def In.epoch.set(i: In, e: String) -> In: + match i: + case In{l, n, h, xs, a, _}: + In{l, n, h, xs, a, e} + +# Optional on the wire: old clients still decode the same far message. +def Fr.with.epoch(+text: String, epoch: String) -> String: + J.Json.show(J.Obj{J.Field{"epoch", J.Str{epoch}, Fr.epoch.fields(Maybe.default(&2, J.Json, J.Json.parse(text), J.Null{}))}}) + +def Fr.msg.epoch(+link: String, +name: String, away: Bool, +since: Nat, +head: Nat, xs: List<&2, Item>, epoch: String) -> String: + J.Json.show(J.Json.obj([J.KV{"t", J.Str{"far"}}, J.KV{"link", J.Str{link}}, J.KV{"name", J.Str{name}}, J.KV{"away", J.Json.bool(away)}, + J.KV{"since", J.Json.nat(since)}, J.KV{"head", J.Json.nat(head)}, J.KV{"epoch", J.Str{epoch}}, J.KV{"items", J.Json.arr(Fr.items.json(xs))}])) + +# all a mirror holds, for a client that just joined +def In.msg(i: In) -> String: + match i: + case In{+l, +n, +h, xs, a, +e}: + Fr.msg.epoch(l, n, a, 0n, h, xs, e) + +# Mirrors by link +# --------------- + +def Ins.find(ms: List<&2, In>, +link: String) -> Maybe<&2, In>: + match ms: + case Nil{}: + None{} + case +i <> t: + Bool.pick(Maybe<&2, In>, String.eq(In.link(i), link), Some{i}, Ins.find(t, link)) + +def Ins.get(+ms: List<&2, In>, +link: String, +name: String) -> In: + Maybe.default(&2, In, Ins.find(ms, link), In.new(link, name)) + +# the mirror of `link` replaced (added when new) +def Ins.set(ms: List<&2, In>, +i: In) -> List<&2, In>: + match ms: + case Nil{}: + [i] + case +x <> +t: + Bool.pick(List<&2, In>, String.eq(In.link(x), In.link(i)), i <> t, x <> Ins.set(t, i)) + +# Requests to a thread elsewhere +# ------------------------------ +# A request naming a thread, an ask or a bot elsewhere goes to the hub it +# lives on, with that hub's ids; only these may (Fr.may): writing to it, +# stopping it, answering it, its queue, settling it, a side question, its +# modes, and a bot's space and routines. Anything else is refused here. + +def Fr.methods() -> List<&2, String>: + ["turn.start", "turn.interrupt", "ask.answer", "queue.move", "queue.remove", "queue.resume", "queue.steer", "queue.edit", + "thread.settle", "thread.unsettle", "thread.btw", "thread.modes", "thread.provider", "thread.effort", "bots.space", "bots.routine_run"] + +def Fr.may.go(ms: List<&2, String>, +m: String) -> Bool: + match ms: + case Nil{}: + False{} + case x <> t: + Bool.or(String.eq(x, m), Fr.may.go(t, m)) + +def Fr.may(+m: String) -> Bool: + Fr.may.go(Fr.methods(), m) + +# the params that name a thing (the ones renamed on the way) +def Fr.keys() -> List<&2, String>: + ["thread", "id", "bot", "routine", "task", "parent", "child", "project", "ask"] + +def Fr.key(+k: String) -> Bool: + Fr.may.go(Fr.keys(), k) + +# the link a request's params name, "" when they name nothing elsewhere +def Fr.params.link(fs: J.Json) -> String: + match fs: + case J.Field{+k, J.Str{+v}, t}: + Bool.pick(String, Bool.and(Fr.key(k), Fr.is(v)), Fr.link(v), Fr.params.link(t)) + case J.Field{_, _, t}: + Fr.params.link(t) + case _: + "" + +# the params with the link's ids made local +def Fr.params.local(fs: J.Json, +link: String) -> J.Json: + match fs: + case J.Field{+k, J.Str{+v}, t}: + J.Field{k, J.Str{Bool.pick(String, Bool.and(Fr.key(k), String.eq(Fr.link(v), link)), Fr.local(v), v)}, Fr.params.local(t, link)} + case J.Field{k, v, t}: + J.Field{k, v, Fr.params.local(t, link)} + case x: + x + +def Fr.obj.fields(o: J.Json) -> J.Json: + match o: + case J.Obj{fs}: + fs + case _: + J.End{} + +def Fr.is.some(m: Maybe<&2, M.Change>) -> Bool: + match m: + case Some{_}: + True{} + case None{}: + False{} + +# a request bound elsewhere: which link, and the request as that hub +# takes it (its method, its params in its ids; no request id) +type Route is Data: + Route{link: String, m: String, body: String} + +def Fr.route.k(+link: String, +m: String, +fs: J.Json) -> Maybe<&2, Route>: + Bool.pick(Maybe<&2, Route>, String.is_empty(link), None{}, + Some{Route{link, m, J.Json.show(J.Json.obj([J.KV{"id", J.Num{"0"}}, J.KV{"m", J.Str{m}}, J.KV{"p", J.Obj{Fr.params.local(fs, link)}}]))}}) + +def Route.link.of(m: Maybe<&2, Route>) -> String: + match m: + case Some{Route{l, _, _}}: + l + case None{}: + "" + +# None: a request for this hub +def Fr.route(+o: J.Json) -> Maybe<&2, Route>: + Fr.route.k(Fr.params.link(Fr.obj.fields(J.Json.get_obj(o, "p"))), J.Json.get_str(o, "m", ""), Fr.obj.fields(J.Json.get_obj(o, "p"))) + +# On the owning hub: a request from a linked machine is taken only when it +# is one of Fr.methods and what it names is shared (a bot's thread, an ask +# in one, a bot) +def Fr.allowed.k(+s: M.State, +m: String, +p: J.Json) -> Bool: + Bool.pick(Bool, String.eq(m, "ask.answer"), Fr.ask.shared(s, J.Json.get_str(p, "id", "")), + Bool.pick(Bool, Bool.or(String.eq(m, "bots.space"), String.eq(m, "bots.routine_run")), Bool.not(String.is_empty(J.Json.get_str(p, "bot", J.Json.get_str(p, "id", "")))), + Fr.thread.shared(s, J.Json.get_str(p, "thread", "")))) + +def Fr.allowed(+s: M.State, +o: J.Json) -> Bool: + Bool.and(Fr.may(J.Json.get_str(o, "m", "")), Fr.allowed.k(s, J.Json.get_str(o, "m", ""), J.Json.get_obj(o, "p"))) + +# The owning hub's reply keeps its success/error and payload; only the +# caller's request id is restored on the receiving hub. +def Fr.reply.fields(fs: J.Json, +id: String) -> J.Json: + match fs: + case J.Field{+k, +v, t}: + J.Field{k, Bool.pick(J.Json, String.eq(k, "id"), J.Num{id}, v), Fr.reply.fields(t, id)} + case x: + x + +def Fr.reply(o: J.Json, +id: String) -> String: + J.Json.show(J.Obj{Fr.reply.fields(Fr.obj.fields(o), id)}) + +# A late disk load never replaces a mirror already updated by traffic. +def In.loaded(+live: In, +disk: In) -> In: + Bool.pick(In, Bool.and(In.away(live), Nat.is_ge(In.head(disk), In.head(live))), disk, live) + +# How a thing elsewhere is shown +# ------------------------------ +# its machine's name beside its own ("Kit · desk"); nothing for one here + +def Fr.where(+id: String, +name: String) -> String: + Bool.pick(String, Fr.is(id), " · " ++ name, "") + +# The owning hub's feed +# --------------------- +# What it shares, as its log numbers it (newest first; each a wire line, +# kept as text so the hub holds no parsed copy): made once from the log +# (off the hub), then grown by each commit. A linked machine's pull reads +# a page of it after the number it has. + +type Line is Data: + Line{n: Nat, text: String} + +def Line.n(l: Line) -> Nat: + match l: + case Line{n, _}: + n + +def Fr.line.k(sh: Bool, +n: Nat, +text: String, rest: List<&2, Line>) -> List<&2, Line>: + match sh: + case True{}: + Line{n, text} <> rest + case False{}: + rest + +def Fr.line.shared(m: Maybe<&2, M.Change>, +s: M.State) -> Bool: + match m: + case Some{c}: + Fr.shared(s, c) + case None{}: + False{} + +def Fr.line.parsed(m: Maybe<&2, J.Json>) -> Maybe<&2, M.Change>: + match m: + case Some{+o}: + C.Change.decode(o) + case None{}: + None{} + +# the shared lines of a log (newest first, `n` the number of the first), +# newest first; tail-recursive (a log holds tens of thousands) +def Fr.feed.go(ls: List<&2, String>, +n: Nat, +s: M.State, acc: List<&2, Line>) -> List<&2, Line>: + match ls: + case Nil{}: + List.reverse(&2, Line, acc) + case +l <> t: + Fr.feed.go(t, Nat.sub(n, 1n), s, Fr.line.k(Fr.line.shared(Fr.line.parsed(J.Json.parse(l)), s), n, l, acc)) + +def Fr.feed(+ls: List<&2, String>, +s: M.State) -> List<&2, Line>: + Fr.feed.go(ls, List.length(&2, String, ls), s, Nil{}) + +# a commit's changes (oldest first) with their wire lines (oldest first), +# numbered after `seq`, the shared ones newest first +def Fr.feed.add(cs: List<&2, M.Change>, ls: List<&2, String>, +seq: Nat, +s: M.State, acc: List<&2, Line>) -> List<&2, Line>: + match cs ls: + case +c <> t +l <> r: + Fr.feed.add(t, r, 1n+seq, s, Fr.line.k(Fr.shared(s, c), 1n+seq, l, acc)) + case _ _: + acc + +def Fr.feed.keep(go: Bool, +n: Nat, +t: String, acc: List<&2, Line>) -> List<&2, Line>: + match go: + case True{}: + Line{n, t} <> acc + case False{}: + acc + +# the lines after `since`, oldest first (the feed is newest first, so they +# are the ones before the first at or below `since`) +def Fr.feed.after(fs: List<&2, Line>, +since: Nat, +go: Bool, acc: List<&2, Line>) -> List<&2, Line>: + match fs: + case Nil{}: + acc + case Line{+n, +t} <> r: + Fr.feed.after(r, since, Bool.and(go, Nat.is_gt(n, since)), Fr.feed.keep(Bool.and(go, Nat.is_gt(n, since)), n, t, acc)) + +# a page: the items after `since` up to about `cap` characters (one bigger +# line goes alone), where it ends (the last item's number when there is +# more, else the log's end), and whether there is more +type Page is Data: + Page{head: Nat, items: List<&2, Item>, more: Bool} + +type Pg is Data: + Pg{room: Nat, last: Nat, count: Nat, items: List<&2, Item>, full: Bool} + +def Fr.page.item(+n: Nat, +t: String) -> Item: + Item{n, Maybe.default(&2, J.Json, J.Json.parse(t), J.Null{})} + +def Fr.page.step.k(fits: Bool, first: Bool, p: Pg, +n: Nat, +t: String) -> Pg: + match fits first p: + case True{} _ Pg{r, _, c, xs, f}: + Pg{Nat.sub(r, String.length(t)), n, 1n+c, Fr.page.item(n, t) <> xs, f} + case False{} True{} Pg{_, _, c, xs, _}: + Pg{0n, n, 1n+c, Fr.page.item(n, t) <> xs, True{}} + case False{} False{} Pg{r, l, c, xs, _}: + Pg{r, l, c, xs, True{}} + +def Fr.page.step(p: Pg, +n: Nat, +t: String) -> Pg: + match p: + case Pg{+r, l, +c, xs, True{}}: + Pg{r, l, c, xs, True{}} + case Pg{+r, l, +c, xs, False{}}: + Fr.page.step.k(Nat.is_le(String.length(t), r), Nat.is_eq(c, 0n), Pg{r, l, c, xs, False{}}, n, t) + +def Fr.page.go(ls: List<&2, Line>, p: Pg) -> Pg: + match ls: + case Nil{}: + p + case Line{+n, +t} <> r: + Fr.page.go(r, Fr.page.step(p, n, t)) + +def Fr.page.of(p: Pg, +total: Nat, +seq: Nat) -> Page: + match p: + case Pg{_, +l, +c, xs, _}: + Page{Bool.pick(Nat, Nat.is_lt(c, total), l, seq), List.reverse(&2, Item, xs), Nat.is_lt(c, total)} + +def Fr.page.k(+ls: List<&2, Line>, +since: Nat, +seq: Nat, +cap: Nat) -> Page: + Fr.page.of(Fr.page.go(ls, Pg{cap, since, 0n, Nil{}, False{}}), List.length(&2, Line, ls), seq) + +# what a pull from `since` gets (seq: this log's length) +def Fr.page(+fs: List<&2, Line>, +since: Nat, +seq: Nat, +cap: Nat) -> Page: + Fr.page.k(Fr.feed.after(fs, since, True{}, Nil{}), since, seq, cap) + +def Page.json(p: Page) -> String: + match p: + case Page{h, xs, m}: + J.Json.show(J.Json.obj([J.KV{"head", J.Json.nat(h)}, J.KV{"items", J.Json.arr(Fr.items.json(xs))}, J.KV{"more", J.Json.bool(m)}])) + +# Pushing to a linked machine +# --------------------------- +# One push at a time per link. A batch says it follows the last push that +# got through (`acked`) only while every push since did (`clean`); after a +# failure (or a restart: nothing is known) it says it follows only its own +# start, so the other end, missing what failed, finds the gap and pulls. +# Batches that come while one is out wait, merged, for it to end. + +type Out is Data: + Out{link: String, acked: Nat, clean: Bool, busy: Bool, from: Nat, to: Nat, items: List<&2, Item>} + +def Out.new(+link: String) -> Out: + Out{link, 0n, False{}, False{}, 0n, 0n, Nil{}} + +def Out.link(o: Out) -> String: + match o: + case Out{l, _, _, _, _, _, _}: + l + +# a batch to send: what it follows, where it ends, its items +type Send is Data: + Send{link: String, since: Nat, to: Nat, items: List<&2, Item>} + +def Out.add.k(empty: Bool, o: Out, +from: Nat, +to: Nat, xs: List<&2, Item>) -> Out: + match empty o: + case True{} Out{l, a, c, b, f, _, ys}: + Out{l, a, c, b, f, to, ys} + case False{} Out{l, a, c, b, f, _, +ys}: + Out{l, a, c, b, Bool.pick(Nat, List.is_empty(&2, Item, ys), from, f), to, List.append(&2, Item, ys, xs)} + +# a commit that took the log from `from` to `to`, with what it shares +def Out.add(o: Out, +from: Nat, +to: Nat, +xs: List<&2, Item>) -> Out: + Out.add.k(List.is_empty(&2, Item, xs), o, from, to, xs) + +def Out.next.k(go: Bool, o: Out) -> Maybe<&2, Send> & Out: + match go o: + case True{} Out{+l, +a, +c, _, +f, +t, xs}: + (Some{Send{l, Bool.pick(Nat, c, a, f), t, xs}}, Out{l, a, c, True{}, f, t, Nil{}}) + case False{} _: + (None{}, o) + +# the push to start now, if one is due (none out, something waiting) +def Out.next(o: Out) -> Maybe<&2, Send> & Out: + match o: + case Out{l, a, c, +b, f, t, +xs}: + Out.next.k(Bool.and(Bool.not(b), Bool.not(List.is_empty(&2, Item, xs))), Out{l, a, c, b, f, t, xs}) + +# the push out ended: through (up to `to`), or not +def Out.done(o: Out, ok: Bool, +to: Nat) -> Out: + match o ok: + case Out{l, _, _, _, f, t, xs} True{}: + Out{l, to, True{}, False{}, f, t, xs} + case Out{l, a, _, _, f, t, xs} False{}: + Out{l, a, False{}, False{}, f, t, xs} + +def Outs.get(os: List<&2, Out>, +link: String) -> Out: + match os: + case Nil{}: + Out.new(link) + case +o <> t: + Bool.pick(Out, String.eq(Out.link(o), link), o, Outs.get(t, link)) + +def Outs.set(os: List<&2, Out>, +o: Out) -> List<&2, Out>: + match os: + case Nil{}: + [o] + case +x <> +t: + Bool.pick(List<&2, Out>, String.eq(Out.link(x), Out.link(o)), o <> t, x <> Outs.set(t, o)) + +def Send.body(s: Send, +name: String) -> String: + match s: + case Send{_, since, to, xs}: + J.Json.show(J.Json.obj([J.KV{"name", J.Str{name}}, J.KV{"since", J.Json.nat(since)}, J.KV{"head", J.Json.nat(to)}, J.KV{"items", J.Json.arr(Fr.items.json(xs))}])) + +# What a hub keeps for threads elsewhere +# -------------------------------------- +# its mirrors (one per link), its pushes (one per link), its own feed +# (None until made from the log), and the lines committed meanwhile + +type Fh is Data: + Fh{ins: List<&2, In>, outs: List<&2, Out>, feed: Maybe<&2, List<&2, Line>>, early: List<&2, Line>, pulls: List<&2, String>, epoch: String} + +def Fh.empty() -> Fh: + Fh{Nil{}, Nil{}, None{}, Nil{}, Nil{}, ""} + +def Fh.ins(f: Fh) -> List<&2, In>: + match f: + case Fh{i, _, _, _, _, _}: + i + +def Fh.in(+f: Fh, +link: String, +name: String) -> In: + Ins.get(Fh.ins(f), link, name) + +def Fh.in.set(f: Fh, i: In) -> Fh: + match f: + case Fh{ms, os, fd, e, p, ep}: + Fh{Ins.set(ms, i), os, fd, e, p, ep} + +def Fh.out(+f: Fh, +link: String) -> Out: + match f: + case Fh{_, os, _, _, _, _}: + Outs.get(os, link) + +def Fh.out.set(f: Fh, o: Out) -> Fh: + match f: + case Fh{ms, os, fd, e, p, ep}: + Fh{ms, Outs.set(os, o), fd, e, p, ep} + +# the feed, None until it is made from the log +def Fh.feed(f: Fh) -> Maybe<&2, List<&2, Line>>: + match f: + case Fh{_, _, fd, _, _, _}: + fd + +def Fh.feed.put(m: Maybe<&2, List<&2, Line>>, e: List<&2, Line>, ls: List<&2, Line>) -> Maybe<&2, List<&2, Line>> & List<&2, Line>: + match m: + case Some{fs}: + (Some{List.append(&2, Line, ls, fs)}, e) + case None{}: + (None{}, List.append(&2, Line, ls, e)) + +def Fh.feed.at.k(f: Fh, fd: Maybe<&2, List<&2, Line>>, e: List<&2, Line>) -> Fh: + match f: + case Fh{ms, os, _, _, p, ep}: + Fh{ms, os, fd, e, p, ep} + +def Fh.feed.at(f: Fh, r: Maybe<&2, List<&2, Line>> & List<&2, Line>) -> Fh: + (fd, e) = r + Fh.feed.at.k(f, fd, e) + +# a commit's shared lines (newest first) join the feed, or wait for it +def Fh.feed.add(f: Fh, ls: List<&2, Line>) -> Fh: + match f: + case Fh{ms, os, fd, e, p, ep}: + Fh.feed.at(Fh{ms, os, None{}, Nil{}, p, ep}, Fh.feed.put(fd, e, ls)) + +# the feed made from the log (up to where it was when asked), with what +# was committed since on top +def Fh.feed.ready(f: Fh, ls: List<&2, Line>) -> Fh: + match f: + case Fh{ms, os, _, e, p, ep}: + Fh{ms, os, Some{List.append(&2, Line, e, ls)}, Nil{}, p, ep} + +def Fh.pulling(+f: Fh, +link: String) -> Bool: + match f: + case Fh{_, _, _, _, p, _}: + Fr.may.go(p, link) + +def Fh.pull.on(f: Fh, +link: String) -> Fh: + match f: + case Fh{ms, os, fd, e, p, ep}: + Fh{ms, os, fd, e, link <> p, ep} + +def Fh.pull.off.go(ps: List<&2, String>, +link: String) -> List<&2, String>: + match ps: + case Nil{}: + Nil{} + case +x <> +t: + Bool.pick(List<&2, String>, String.eq(x, link), t, x <> Fh.pull.off.go(t, link)) + +def Fh.pull.off(f: Fh, +link: String) -> Fh: + match f: + case Fh{ms, os, fd, e, p, ep}: + Fh{ms, os, fd, e, Fh.pull.off.go(p, link), ep} + +def Fh.epoch(f: Fh) -> String: + match f: + case Fh{_, _, _, _, _, ep}: + ep + +def Fh.epoch.set(f: Fh, +epoch: String) -> Fh: + match f: + case Fh{ms, os, fd, e, p, +ep}: + Fh{ms, os, fd, e, p, Bool.pick(String, String.is_empty(epoch), ep, epoch)} + +# lines (newest first) as items, oldest first +def Fr.lines.items(ls: List<&2, Line>, acc: List<&2, Item>) -> List<&2, Item>: + match ls: + case Nil{}: + acc + case Line{+n, +t} <> r: + Fr.lines.items(r, Fr.page.item(n, t) <> acc) + +# A mirror kept on disk +# --------------------- +# /far/.jsonl: each batch taken, its items as lines, then +# {"head": n}; read back at start (the mirror shows, away, until its +# machine answers) + +def Fr.file.items(xs: List<&2, Item>) -> String: + match xs: + case Nil{}: + "" + case x <> t: + J.Json.show(Item.json(x)) ++ "\n" ++ Fr.file.items(t) + +def Fr.file.batch(xs: List<&2, Item>, +head: Nat) -> String: + Fr.file.items(xs) ++ "{\"head\":" ++ Nat.show(head) ++ "}\n" + +def Bool.is_some.j(m: Maybe<&2, J.Json>) -> Bool: + match m: + case Some{_}: + True{} + case None{}: + False{} + +def Fr.some.j(m: Maybe<&2, J.Json>) -> Bool: + match m: + case Some{_}: + True{} + case None{}: + False{} + +# New files carry an epoch header before their batches. Old files remain +# readable; the first epoch-aware pull replaces that legacy projection. +def Fr.file.line(m: Maybe<&2, J.Json>, i: In) -> In: + match m i: + case Some{+o} In{+l, +n, +h, +xs, +a, +e}: + Bool.pick(In, Fr.some.j(J.Json.get(o, "c")), In{l, n, h, Item.of(o) <> xs, a, e}, + In{l, n, J.Json.get_nat(o, "head", h), xs, a, J.Json.get_str(o, "epoch", e)}) + case None{} _: + i + +def Fr.file.scan(ls: List<&2, String>, i: In) -> In: + match ls: + case Nil{}: + i + case l <> t: + Fr.file.scan(t, Fr.file.line(J.Json.parse(l), i)) + +def Fr.file.done(i: In) -> In: + match i: + case In{l, n, h, xs, a, e}: + In{l, n, h, List.reverse(&2, Item, xs), a, e} + +def Fr.file.read(+text: String, +link: String, +name: String) -> In: + Fr.file.done(Fr.file.scan(String.lines(text), In.new(link, name))) + +def In.name.of(i: In) -> String: + match i: + case In{_, n, _, _, _, _}: + n + +# a push that only says where it ends, as a gap: the other end pulls +def Send.gap(s: Send) -> Send: + match s: + case Send{l, _, +to, _}: + Send{l, to, to, Nil{}} + +# a mirrored bot's space page (its newest SpacePageSet; "" for none), for +# GET /bots/~/space.html on the hub that mirrors it +def Fr.page.html.k(hit: Bool, +o: J.Json, +cur: String) -> String: + match hit: + case True{}: + J.Json.get_str(o, "html", "") + case False{}: + cur + +def Fr.page.html.go(xs: List<&2, Item>, +bot: String, +cur: String) -> String: + match xs: + case Nil{}: + cur + case Item{_, +c} <> t: + Fr.page.html.go(t, bot, Fr.page.html.k(Bool.and(String.eq(J.Json.get_str(c, "$", ""), "SpacePageSet"), String.eq(J.Json.get_str(c, "bot", ""), bot)), c, cur)) + +def In.page(i: In, +bot: String) -> String: + match i: + case In{_, _, _, xs, _, _}: + Fr.page.html.go(xs, bot, "") diff --git a/src/core/refs.bend b/src/core/refs.bend index d5738073b5..e789875511 100644 --- a/src/core/refs.bend +++ b/src/core/refs.bend @@ -462,7 +462,7 @@ def Refs.bots.go(bs: List<&2, BT.Bot>, +s: M.State, +sel: String, +now: Nat, +q: Refs.b.cand(s, sel, now, q, b, Refs.bots.go(r, s, sel, now, q)) def Refs.bots(+s: M.State, +sel: String, +now: Nat, +q: String) -> List<&2, Cand>: - Refs.bots.go(BT.World.bots(M.State.bots(s)), s, sel, now, q) + Refs.bots.go(BT.World.bots.here(M.State.bots(s)), s, sel, now, q) # Bots on linked machines, as the hub lists them (info "bots.remote": # [{"name", "peer", "peerName", "state", "seen"}]): a bot's word is @@ -654,7 +654,7 @@ def Refs.r.hits(rs: List<&2, BT.Room>, +w: String) -> List<&2, Hit>: def Refs.find.k(i: Nat, +s: M.State, +sel: String, +w: String) -> List<&2, Hit>: match i: case 0n: - Refs.b.hits(BT.World.bots(M.State.bots(s)), w, sel) + Refs.b.hits(BT.World.bots.here(M.State.bots(s)), w, sel) case 1n: Refs.find.thread(s, sel, w) case 2n: diff --git a/src/gfx/text.bend b/src/gfx/text.bend index 1c890993a0..9fdbc1724e 100644 --- a/src/gfx/text.bend +++ b/src/gfx/text.bend @@ -368,6 +368,19 @@ def Fit.count.k(ok: Bool, rest: Nat) -> Nat: case False{}: 0n +# The decision belongs to the character just examined. Branch before +# touching the suffix: eager arguments to Fit.count.k walked all of it, +# even when the first character overflowed. +def Fit.count.go(s: String, fits: Bool, +cache: Map<&2, TG>, +f: Face, +max: U32, +used: U32) -> Nat: + match s fits: + case _ False{}: + 0n + case SNil{} True{}: + 1n + case SCon{Chr{+cp}, t} True{}: + +u2 = (used + TG.adv(Text.get(cache, f, cp)) : U32) + 1n+Fit.count.go(t, U32.is_le(u2, max), cache, f, max, u2) + # how many leading characters of s fit in max (26.6) after used def Fit.count(s: String, +cache: Map<&2, TG>, +f: Face, +max: U32, +used: U32) -> Nat: match s: @@ -375,16 +388,30 @@ def Fit.count(s: String, +cache: Map<&2, TG>, +f: Face, +max: U32, +used: U32) - 0n case SCon{Chr{+cp}, t}: +u2 = (used + TG.adv(Text.get(cache, f, cp)) : U32) - Fit.count.k(U32.is_le(u2, max), Fit.count(t, cache, f, max, u2)) + Fit.count.go(t, U32.is_le(u2, max), cache, f, max, u2) -# s in pieces that each fit in max (at least one character each) -def Fit.chunk(n: Nat, empty: Bool, +s: String, +cache: Map<&2, TG>, +f: Face, +max: U32) -> List<&2, String>: +# Always make progress, even when a single glyph is wider than the line. +def Fit.atleast(n: Nat) -> Nat: + match n: + case 0n: + 1n + case 1n+k: + 1n+k + +# Pieces newest first, so even a token broken into many narrow lines +# never builds a call stack proportional to the number of lines. +def Fit.chunk.go(n: Nat, empty: Bool, +s: String, +cache: Map<&2, TG>, +f: Face, +max: U32, acc: List<&2, String>) -> List<&2, String>: match n empty: case 1n+m False{}: - +k = Bool.pick(Nat, Nat.is_eq(Fit.count(s, cache, f, max, 0), 0n), 1n, Fit.count(s, cache, f, max, 0)) - String.take(s, k) <> Fit.chunk(m, String.is_empty(String.drop(s, k)), String.drop(s, k), cache, f, max) + +k = Fit.atleast(Fit.count(s, cache, f, max, 0)) + +rest = String.drop(s, k) + Fit.chunk.go(m, String.is_empty(rest), rest, cache, f, max, String.take(s, k) <> acc) case _ _: - Nil{} + List.reverse(&2, String, acc) + +# s in pieces that each fit in max (at least one character each) +def Fit.chunk(n: Nat, empty: Bool, +s: String, +cache: Map<&2, TG>, +f: Face, +max: U32) -> List<&2, String>: + Fit.chunk.go(n, empty, s, cache, f, max, Nil{}) # lines so far (newest first), the line being filled, and its advance type PAcc is Data: diff --git a/src/mobile/bots.bend b/src/mobile/bots.bend index 819f7a95e0..181e21123a 100644 --- a/src/mobile/bots.bend +++ b/src/mobile/bots.bend @@ -34,10 +34,15 @@ def MBots.ago(none: Bool, +now: Nat, +at: Nat) -> String: # The list # -------- -def MBots.row(r: B.BRow) -> J.Json: +# a cat in the list; "act" is what a tap sends with its id (BU.act: "bot" +# for a bot with a thread, here or mirrored, "remote" for one only +# reported; "remote" stays for apps that pick it themselves), "title" its +# name as shown (with its machine when elsewhere) +def MBots.row(+r: B.BRow) -> J.Json: match r: case B.BRow{+i, +n, _, +lk, +m, note, sel, +rem, peer, +l}: - J.Json.obj([J.KV{"id", J.Json.str(i)}, J.KV{"name", J.Json.str(n)}, J.KV{"look", J.Json.nat(lk)}, + J.Json.obj([J.KV{"id", J.Json.str(i)}, J.KV{"name", J.Json.str(n)}, J.KV{"title", J.Json.str(B.BU.row.title(r))}, + J.KV{"act", J.Json.str(B.BU.act(r))}, J.KV{"look", J.Json.nat(lk)}, J.KV{"mood", J.Json.str(m)}, J.KV{"note", J.Json.str(note)}, J.KV{"sel", J.Json.bool(sel)}, J.KV{"remote", J.Json.bool(rem)}, J.KV{"peer", J.Json.str(peer)}, J.KV{"cat", MBots.cat(lk, m)}, J.KV{"key", J.Json.str(B.BU.key.k(rem, i, n, l))}, J.KV{"link", J.Json.str(l)}]) @@ -245,17 +250,24 @@ def MBots.tab.k(i: Nat, +u: U.Ui, b: BT.Bot, +id: String, +persona: String, +now case _: Nil{} -def MBots.bot.m(+u: U.Ui, b: BT.Bot, +id: String, +name: String, +lk: Nat, +persona: String, +mood: String, +tab: String) -> J.Json: +# a bot's header (BU.head): its name as shown ("title": with its machine +# when mirrored from a linked machine), its mood, its tabs and the one +# shown, and whether its memory and routines may be changed here ("edit") +def MBots.head(+hd: B.Head) -> List<&2, J.KV>: + [J.KV{"title", J.Json.str(B.Head.title(hd))}, J.KV{"mood", J.Json.str(B.Head.mood(hd))}, + J.KV{"note", J.Json.str(B.Head.note(hd))}, J.KV{"look", J.Json.nat(B.Head.look(hd))}, J.KV{"cat", MBots.cat(B.Head.look(hd), B.Head.mood(hd))}, + J.KV{"tab", J.Json.str(B.Head.tab(hd))}, J.KV{"tabs", J.Json.arr(MBots.tabs(B.Head.tabs(hd)))}, + J.KV{"edit", J.Json.bool(B.Head.owns(hd))}] + +def MBots.bot.m(+u: U.Ui, b: BT.Bot, +id: String, +name: String, +persona: String, +hd: B.Head) -> J.Json: J.Json.obj(List.append(&2, J.KV, - [J.KV{"kind", J.Json.str("bot")}, J.KV{"id", J.Json.str(id)}, J.KV{"name", J.Json.str(name)}, J.KV{"mood", J.Json.str(mood)}, - J.KV{"note", J.Json.str(B.Mood.note(mood))}, J.KV{"look", J.Json.nat(lk)}, J.KV{"cat", MBots.cat(lk, mood)}, - J.KV{"tab", J.Json.str(tab)}, J.KV{"tabs", J.Json.arr(MBots.tabs(B.BU.tabs()))}], - MBots.tab.k(C.Codec.pos(B.BU.tabs(), tab), u, b, id, persona, U.Side.now(u)))) + [J.KV{"kind", J.Json.str("bot")}, J.KV{"id", J.Json.str(id)}, J.KV{"name", J.Json.str(name)}], + List.append(&2, J.KV, MBots.head(hd), MBots.tab.k(C.Codec.pos(B.BU.tabs(), B.Head.tab(hd)), u, b, id, persona, U.Side.now(u))))) def MBots.bot(+u: U.Ui, b: BT.Bot) -> J.Json: match b: case BT.Bot{+i, +n, +th, +lk, +pe, +c, +d}: - MBots.bot.m(u, BT.Bot{i, n, th, lk, pe, c, d}, i, n, lk, pe, B.Mood.bot(U.Ui.state(u), U.Side.now(u), BT.Bot{i, n, th, lk, pe, c, d}), B.BU.tab(u)) + MBots.bot.m(u, BT.Bot{i, n, th, lk, pe, c, d}, i, n, pe, B.BU.head.bot(u, BT.Bot{i, n, th, lk, pe, c, d})) # Rooms and bots elsewhere # ------------------------ @@ -282,35 +294,18 @@ def MBots.room.view(+u: U.Ui, r: BT.Room) -> J.Json: J.KV{"posts", J.Json.arr(MBots.posts(B.BU.posts(u, i), u, U.Side.now(u)))}, J.KV{"draft", J.Json.str(B.BU.field(u, "rpost"))}]) -def MBots.far.o(+u: U.Ui, +id: String, name: String, +mood: String, note: String, +lk: Nat, peer: String) -> J.Json: - J.Json.obj([J.KV{"kind", J.Json.str("remote")}, J.KV{"id", J.Json.str(id)}, J.KV{"name", J.Json.str(name)}, - J.KV{"mood", J.Json.str(mood)}, J.KV{"note", J.Json.str(note)}, J.KV{"look", J.Json.nat(lk)}, J.KV{"cat", MBots.cat(lk, mood)}, - J.KV{"peer", J.Json.str(peer)}, J.KV{"posts", J.Json.arr(MBots.posts(B.BU.remote.posts(u, id), u, U.Side.now(u)))}, - J.KV{"draft", J.Json.str(B.BU.field(u, "rtext"))}]) - -def MBots.far.m(m: Maybe<&2, B.BRow>, +u: U.Ui, +id: String) -> J.Json: +def MBots.far.m(m: B.BRow, +u: U.Ui, +id: String, +hd: B.Head) -> J.Json: match m: - case Some{B.BRow{_, n, _, lk, mo, note, _, _, peer, _}}: - MBots.far.o(u, id, n, mo, note, lk, peer) - case None{}: - MBots.far.o(u, id, id, "away", B.Mood.note("away"), 0n, "") - -def MBots.rid(r: B.BRow) -> String: - match r: - case B.BRow{i, _, _, _, _, _, _, _, _, _}: - i - -def MBots.far.find(rs: List<&2, B.BRow>, +id: String) -> Maybe<&2, B.BRow>: - match rs: - case Nil{}: - None{} - case +r <> t: - Bool.pick(Maybe<&2, B.BRow>, String.eq(MBots.rid(r), id), Some{r}, MBots.far.find(t, id)) - -# a bot on a linked machine: its cat and the person's conversation with -# it ("bfield" rtext, "remote-send") + case B.BRow{_, n, _, _, _, _, _, _, peer, _}: + J.Json.obj(List.append(&2, J.KV, [J.KV{"kind", J.Json.str("remote")}, J.KV{"id", J.Json.str(id)}, J.KV{"name", J.Json.str(n)}, + J.KV{"peer", J.Json.str(peer)}, J.KV{"posts", J.Json.arr(MBots.posts(B.BU.remote.posts(u, id), u, U.Side.now(u)))}, + J.KV{"draft", J.Json.str(B.BU.field(u, "rtext"))}], MBots.head(hd))) + +# a bot only reported by a linked machine: the same header as a bot's +# (BU.head: "name · machine", Chat alone) over the person's conversation +# with it ("bfield" rtext, "remote-send") def MBots.far(+u: U.Ui, +id: String) -> J.Json: - MBots.far.m(MBots.far.find(B.BU.remote(u), id), u, id) + MBots.far.m(B.BU.remote.row(u, id), u, id, B.BU.head(u, B.VRemote{id})) def MBots.view(v: B.View, +u: U.Ui) -> J.Json: match v: diff --git a/src/mobile/hubs.bend b/src/mobile/hubs.bend index 22ade8123f..3f784a9749 100644 --- a/src/mobile/hubs.bend +++ b/src/mobile/hubs.bend @@ -10,6 +10,7 @@ import ./picker.bend as PK import ../core/botui.bend as BU import ../core/bot.bend as BT import ../core/once.bend as O +import ../core/far.bend as FR # The phone keeps every hub it paired with connected at once, each with # its own client (Ui). A hub's key is its socket's host:port, which the @@ -446,6 +447,14 @@ def Hubs.routes() -> Nat: def Hubs.everywhere(+action: String) -> Bool: Nat.is_lt(C.Codec.pos(["dismiss", "delete-no", "proj-keep", "proj-find", "proj-find-q", "menu-close", "side-older", "side-settled"], action), 8n) +# The merged phone sidebar has one view preference across paired hubs. +def Hubs.every.op(op: Op) -> Bool: + match op: + case Do{+a, +v}: + Bool.or(Hubs.everywhere(a), Bool.and(String.eq(a, "setting"), String.starts_with(v, "sidebar.view="))) + case _: + False{} + # of what the hub let go says, only what it sends (its viewer's watch # ends); its focus and scroll are for a screen no longer shown def Hubs.sends(cs: List<&2, HCmd>) -> List<&2, HCmd>: @@ -481,7 +490,7 @@ def Hubs.focus(move: Bool, hs: Hubs, +key: String, +op: Op) -> HAct: case True{} Hubs{+cid, +now, +cur, list}: Hubs.then(Hubs.run(list, False{}, cur, Do{"select", ""}), cid, now, key, key, op) case False{} Hubs{+cid, +now, +cur, list}: - Hubs.step(Hubs{cid, now, cur, list}, Hubs.everywhere(Op.action(op)), key, op) + Hubs.step(Hubs{cid, now, cur, list}, Hubs.every.op(op), key, op) # (matches, not Bool.pick: an unrouted value, which may be a big piece of # an upload, is never split) @@ -685,6 +694,91 @@ def Hubs.projects.p(+ps: List<&2, J.Json>) -> List<&2, J.Json>: def Hubs.projects(ss: List<&2, Shown>, +many: Bool) -> List<&2, J.Json>: Hubs.projects.p(Hubs.projects.all(ss, many)) +# Once a paired owner replaces a mirror, keep its machine visible too. +# A mirror already names its origin in its title; its relay is not its +# origin. Rows from the focused hub retain the usual collision label. +def Hubs.origin.name(ss: List<&2, Shown>, +key: String) -> String: + match ss: + case Nil{}: + key + case Shown{+k, +n, _} <> t: + Bool.pick(String, String.eq(k, key), n, Hubs.origin.name(t, key)) + +def Hubs.origin.project(elsewhere: Bool, p: J.Json, +name: String) -> J.Json: + match elsewhere: + case True{}: + Hubs.machine(p, name) + case False{}: + p + +def Hubs.origin.projects(ps: List<&2, J.Json>, +ss: List<&2, Shown>, +cur: String) -> List<&2, J.Json>: + match ps: + case Nil{}: + Nil{} + case +p <> t: + +id = J.Json.get_str(p, "id", "") + Hubs.origin.project(Bool.and(Bool.not(String.eq(Hubs.key_of(id), cur)), Bool.not(FR.Fr.is(Hubs.rest(id)))), p, + Hubs.origin.name(ss, Hubs.key_of(id))) <> Hubs.origin.projects(t, ss, cur) + +# A directly paired owner survives mirror deduplication. Its Active rows +# and focused headers still name their machine, including when its socket +# is offline. Far and reported-only titles already name their origin. +def Hubs.origin.caption(online: Bool, title: String, name: String) -> String: + match online: + case True{}: + title ++ " · " ++ name + case False{}: + title ++ " · " ++ name ++ " (away)" + +def Hubs.origin.title(mark: Bool, title: String, +name: String, +online: Bool) -> String: + match mark: + case False{}: + title + case True{}: + Hubs.origin.caption(online, title, name) + +def Hubs.origin.fields(fs: J.Json, +name: String, +online: Bool) -> J.Json: + match fs: + case J.Field{+k, J.Str{+v}, t}: + J.Field{k, J.Str{Hubs.origin.title(String.eq(k, "title"), v, name, online)}, Hubs.origin.fields(t, name, online)} + case J.Field{k, v, t}: + J.Field{k, v, Hubs.origin.fields(t, name, online)} + case x: + x + +def Hubs.origin.object(v: J.Json, +name: String, +online: Bool) -> J.Json: + match v: + case J.Obj{fs}: + J.Obj{Hubs.origin.fields(fs, name, online)} + case _: + v + +def Hubs.origin.label(mark: Bool, v: J.Json, name: String, online: Bool) -> J.Json: + match mark: + case False{}: + v + case True{}: + Hubs.origin.object(v, name, online) + +def Hubs.origin.direct(+v: J.Json) -> Bool: + +id = J.Json.get_str(v, "id", "") + Bool.not(Bool.or(Bool.or(FR.Fr.is(id), FR.Fr.is(Hubs.rest(id))), + Bool.or(J.Json.get_bool(v, "remote", False{}), String.eq(J.Json.get_str(v, "kind", ""), "remote")))) + +def Hubs.origin.row(+many: Bool, +v: J.Json, +name: String, +online: Bool) -> J.Json: + Hubs.origin.label(Bool.and(many, Hubs.origin.direct(v)), v, name, online) + +def Hubs.origin.rows(rs: List<&2, J.Json>, +many: Bool, +name: String, +online: Bool) -> List<&2, J.Json>: + match rs: + case Nil{}: + Nil{} + case r <> t: + Hubs.origin.row(many, r, name, online) <> Hubs.origin.rows(t, many, name, online) + +def Hubs.origin.focus(+ss: List<&2, Shown>, +cur: String, +field: String) -> J.Json: + Hubs.origin.row(Nat.is_gt(List.length(&2, Shown, ss), 1n), J.Json.get_obj(Hubs.of(ss, cur), field), + Hubs.origin.name(ss, cur), J.Json.get_bool(Hubs.of(ss, cur), "online", False{})) + # every hub's folded shelf at k ("older": older projects; "settled": the # active view's settled rows) as one: how many in all, and open when any # hub's is ("side-older", "side-settled" go to every hub) @@ -718,15 +812,16 @@ def Hubs.arow.cmp(+a: J.Json, +b: J.Json) -> Bool: def Hubs.arow.before(a: J.Json, b: J.Json) -> Bool: Hubs.arow.cmp(a, b) -def Hubs.active.all(ss: List<&2, Shown>) -> List<&2, J.Json>: +def Hubs.active.all(ss: List<&2, Shown>, +many: Bool) -> List<&2, J.Json>: match ss: case Nil{}: Nil{} - case Shown{_, _, +j} <> t: - List.append(&2, J.Json, J.Json.elems(J.Json.get_obj(j, "active")), Hubs.active.all(t)) + case Shown{_, +name, +j} <> t: + List.append(&2, J.Json, Hubs.origin.rows(J.Json.elems(J.Json.get_obj(j, "active")), many, name, + J.Json.get_bool(j, "online", False{})), Hubs.active.all(t, many)) -def Hubs.active(ss: List<&2, Shown>) -> List<&2, J.Json>: - List.sort(~J.Json, ~Hubs.arow.before, Hubs.active.all(ss)) +def Hubs.active(+ss: List<&2, Shown>) -> List<&2, J.Json>: + List.sort(~J.Json, ~Hubs.arow.before, Hubs.active.all(ss, Nat.is_gt(List.length(&2, Shown, ss), 1n))) # every hub's list at k (bots, rooms), named by machine when there are several def Hubs.all(ss: List<&2, Shown>, +k: String, +many: Bool) -> List<&2, J.Json>: @@ -783,7 +878,9 @@ def Hubs.auth(+url: String) -> String: # the machine at its url) and has a bot of that name, as that hub's own # bot; else by its machine's host:port and its name, so two hubs reporting # the same machine agree. Every hub's own bots come first, so a bot shows -# as its own hub's. +# as its own hub's. A bot a hub mirrors from a linked machine (its threads +# kept there, far.bend) is dropped when that machine is paired too +# (Hubs.bot.dup): it shows once, as its own hub's. def Hubs.state(h: Hub) -> M.State: U.Ui.state(Hub.ui(h)) @@ -844,8 +941,151 @@ def Hubs.bot.key(+hs: List<&2, Hub>, +from: String, +r: J.Json) -> String: Hubs.far.key(hs, from, Hubs.far.at(Hubs.of.key(hs, from), J.Json.get_str(r, "link", "")), r), from ++ "|" ++ J.Json.get_str(r, "key", "")) +# whether a paired hub other than `from` holds link l: a link's id is the +# same at both its ends, so that hub is the machine l reaches from `from` +# (or, for a link among more, one that reaches it too) +def Hubs.holds(hs: List<&2, Hub>, +from: String, +l: String) -> Bool: + match hs: + case Nil{}: + False{} + case +h <> t: + Bool.or(Bool.and(Bool.not(String.eq(Hub.key(h), from)), BT.Maybe.is(BT.Peer, BT.Peer.by.id(BT.World.peers(M.State.bots(Hubs.state(h))), l))), + Hubs.holds(t, from, l)) + +# Projects and ordinary threads follow the same ownership rule as bots. +# Drop a mirror only when the paired owner actually has that entity; +# during initial connection its cached mirror must remain visible. +def Hubs.entity.got(-A: Data, r: Map<&2, A> & Bool) -> Bool: + (_, b) = r + b + +def Hubs.entity.has(+s: M.State, project: Bool, +id: String) -> Bool: + match s project: + case M.State{ps, _, _, _, _, _, _, _, _, _, _} True{}: + Hubs.entity.got(M.Project, Map.has(&2, M.Project, ps, id)) + case M.State{_, ts, _, _, _, _, _, _, _, _, _} False{}: + Hubs.entity.got(M.Thread, Map.has(&2, M.Thread, ts, id)) + +def Hubs.entity.owner(hs: List<&2, Hub>, +from: String, +link: String, +at: String, +project: Bool, +id: String) -> Bool: + match hs: + case Nil{}: + False{} + case +h <> t: + Bool.or(Bool.and(Hubs.twin.is(Hub.key(h), Hubs.state(h), from, link, at), Hubs.entity.has(Hubs.state(h), project, id)), + Hubs.entity.owner(t, from, link, at, project, id)) + +def Hubs.entity.dup(+hs: List<&2, Hub>, +from: String, +project: Bool, +id: String) -> Bool: + Bool.and(FR.Fr.is(id), Hubs.entity.owner(hs, from, FR.Fr.link(id), Hubs.far.at(Hubs.of.key(hs, from), FR.Fr.link(id)), project, FR.Fr.local(id))) + +def Hubs.catalog.rows(rs: List<&2, J.Json>, +hs: List<&2, Hub>, +from: String, +project: Bool) -> List<&2, J.Json>: + match rs: + case Nil{}: + Nil{} + case +r <> t: + Hubs.part.k(Bool.not(Hubs.entity.dup(hs, from, project, Hubs.rest(J.Json.get_str(r, "id", "")))), r, + Hubs.catalog.rows(t, hs, from, project)) + +# Filter only catalog arrays, never timeline text or the focused view. +# A mirrored project contains only mirrored threads, so dropping its +# section also drops every shelf without touching local fold state. +def Hubs.catalog.value(catalog: Bool, v: J.Json, +hs: List<&2, Hub>, +from: String, +project: Bool) -> J.Json: + match catalog: + case True{}: + J.Json.arr(Hubs.catalog.rows(J.Json.elems(v), hs, from, project)) + case False{}: + v + +# Fold counts include hidden rows. Apply the ownership filter to those +# rows too, so pairing the owner does not double Older or Settled. +def Hubs.fold.projects(rs: List<&2, U.SRow>, +hs: List<&2, Hub>, +from: String, acc: Nat) -> Nat: + match rs: + case Nil{}: + acc + case U.SRow{_, M.Project{+id, _, _, _, _}, _} <> t: + Hubs.fold.projects(t, hs, from, Nat.add(acc, Bool.pick(Nat, Hubs.entity.dup(hs, from, True{}, id), 1n, 0n))) + +def Hubs.fold.older(g: U.SGroups, +hs: List<&2, Hub>, +from: String) -> Nat: + match g: + case U.SGroups{_, _, o}: + Hubs.fold.projects(o, hs, from, 0n) + +def Hubs.fold.rows(rs: List<&2, U.ARow>, +hs: List<&2, Hub>, +from: String, acc: Nat) -> Nat: + match rs: + case Nil{}: + acc + case U.ARow{_, _, +t, _, _, _} <> rest: + Hubs.fold.rows(rest, hs, from, Nat.add(acc, Bool.pick(Nat, Hubs.entity.dup(hs, from, False{}, M.Thread.id(t)), 1n, 0n))) + +def Hubs.fold.settled(p: U.AParts, +hs: List<&2, Hub>, +from: String) -> Nat: + match p: + case U.AParts{_, f}: + Hubs.fold.rows(f, hs, from, 0n) + +def Hubs.fold.count(older: Bool, h: Maybe<&2, Hub>, +hs: List<&2, Hub>, +from: String) -> Nat: + match older h: + case True{} Some{Hub{_, U.Ui{+st, _, +dr, _, +o, _, +now, _, _, _, _, _, _, _, _, _, _}}}: + Hubs.fold.older(U.Side.groups(U.Side.shown(st, now, U.Find.q(o, dr)), now), hs, from) + case False{} Some{Hub{_, U.Ui{+st, _, +dr, _, +o, _, +now, _, _, _, _, _, _, _, _, _, _}}}: + Hubs.fold.settled(U.Active.parts(U.Active.rows(st, now, U.Find.q(o, dr))), hs, from) + case _ None{}: + 0n + +def Hubs.fold.json(v: J.Json, +older: Bool, +hs: List<&2, Hub>, +from: String) -> J.Json: + match v: + case J.Obj{+fs}: + S.Mob.older.j(Nat.sub(J.Json.get_nat(J.Obj{fs}, "count", 0n), Hubs.fold.count(older, Hubs.find(hs, from), hs, from)), + J.Json.get_bool(J.Obj{fs}, "open", False{}), "") + case x: + x + +def Hubs.catalog.field(kind: Nat, v: J.Json, +hs: List<&2, Hub>, +from: String) -> J.Json: + match kind: + case 0n: + Hubs.catalog.value(True{}, v, hs, from, True{}) + case 1n: + Hubs.catalog.value(True{}, v, hs, from, False{}) + case 2n: + Hubs.fold.json(v, True{}, hs, from) + case 3n: + Hubs.fold.json(v, False{}, hs, from) + case _: + v + +def Hubs.catalog.fields(fs: J.Json, +hs: List<&2, Hub>, +from: String) -> J.Json: + match fs: + case J.Field{+k, +v, t}: + J.Field{k, Hubs.catalog.field(C.Codec.pos(["projects", "active", "older", "settled"], k), v, hs, from), Hubs.catalog.fields(t, hs, from)} + case x: + x + +def Hubs.catalog.shown(ss: List<&2, Shown>, +hs: List<&2, Hub>) -> List<&2, Shown>: + match ss: + case Nil{}: + Nil{} + case Shown{+k, n, J.Obj{fs}} <> t: + Shown{k, n, J.Obj{Hubs.catalog.fields(fs, hs, k)}} <> Hubs.catalog.shown(t, hs) + case h <> t: + h <> Hubs.catalog.shown(t, hs) + +# a row a hub mirrors from a linked machine (far.bend): a bot elsewhere +# that opens as a bot (BU.act), its threads kept by this hub +def Hubs.mirrored(+r: J.Json) -> Bool: + Bool.and(J.Json.get_bool(r, "remote", False{}), String.eq(J.Json.get_str(r, "act", ""), "bot")) + +# a mirrored bot whose own machine is paired too: that hub lists it as its +# own, so the copy goes (law hubs_bots_mirror_dropped) +def Hubs.bot.dup(+hs: List<&2, Hub>, +from: String, +r: J.Json) -> Bool: + Bool.and(Hubs.mirrored(r), Bool.and(Bool.not(String.is_empty(J.Json.get_str(r, "link", ""))), Hubs.holds(hs, from, J.Json.get_str(r, "link", "")))) + +def Hubs.bot.keep.k(dup: Bool, +far: Bool, +want: Bool, +hs: List<&2, Hub>, +from: String, +r: J.Json, rest: List<&2, O.Ky>) -> List<&2, O.Ky>: + match dup: + case True{}: + rest + case False{}: + BT.Post.keep(O.Ky, Bool.pick(Bool, want, far, Bool.not(far)), O.Ky{Hubs.bot.key(hs, from, r), r}, rest) + def Hubs.bot.keep(+far: Bool, +want: Bool, +hs: List<&2, Hub>, +from: String, +r: J.Json, rest: List<&2, O.Ky>) -> List<&2, O.Ky>: - BT.Post.keep(O.Ky, Bool.pick(Bool, want, far, Bool.not(far)), O.Ky{Hubs.bot.key(hs, from, r), r}, rest) + Hubs.bot.keep.k(Hubs.bot.dup(hs, from, r), far, want, hs, from, r, rest) def Hubs.bot.rows(rs: List<&2, J.Json>, +want: Bool, +hs: List<&2, Hub>, +from: String) -> List<&2, O.Ky>: match rs: @@ -925,7 +1165,7 @@ def Hubs.screen.k(+ss: List<&2, Shown>, +cur: String, +list: List<&2, Hub>) -> J J.KV{"sel", J.Json.str(J.Json.get_str(Hubs.of(ss, cur), "sel", ""))}, J.KV{"theme", J.Json.str(J.Json.get_str(Hubs.of(ss, cur), "theme", ""))}, J.KV{"empty", J.Json.str(Hubs.empty(List.length(&2, J.Json, Hubs.projects(ss, False{})), List.length(&2, Shown, ss), J.Json.get_bool(Hubs.first_obj(ss, "search"), "open", False{})))}, - J.KV{"projects", J.Json.arr(Hubs.projects(ss, Nat.is_gt(List.length(&2, Shown, ss), 1n)))}, + J.KV{"projects", J.Json.arr(Hubs.origin.projects(Hubs.projects(ss, Nat.is_gt(List.length(&2, Shown, ss), 1n)), ss, cur))}, J.KV{"older", Hubs.older(ss, "older")}, J.KV{"settled", Hubs.older(ss, "settled")}, J.KV{"view", J.Json.str(J.Json.get_str(Hubs.of(ss, cur), "view", "projects"))}, @@ -938,12 +1178,12 @@ def Hubs.screen.k(+ss: List<&2, Shown>, +cur: String, +list: List<&2, Hub>) -> J J.KV{"settings", J.Json.get_obj(Hubs.of(ss, cur), "settings")}, J.KV{"find", J.Json.get_obj(Hubs.of(ss, cur), "find")}, J.KV{"island", N.Note.island.k(Hubs.lines(ss))}, - J.KV{"thread", J.Json.get_obj(Hubs.of(ss, cur), "thread")}, + J.KV{"thread", Hubs.origin.focus(ss, cur, "thread")}, J.KV{"bots", J.Json.arr(Hubs.bots(ss, list, Nat.is_gt(List.length(&2, Shown, ss), 1n)))}, J.KV{"rooms", J.Json.arr(Hubs.rooms(ss, Nat.is_gt(List.length(&2, Shown, ss), 1n)))}, J.KV{"newBot", J.Json.get_obj(Hubs.of(ss, cur), "newBot")}, J.KV{"newRoom", J.Json.get_obj(Hubs.of(ss, cur), "newRoom")}, - J.KV{"bot", J.Json.get_obj(Hubs.of(ss, cur), "bot")}, + J.KV{"bot", Hubs.origin.focus(ss, cur, "bot")}, J.KV{"hub", J.Json.str(cur)}, J.KV{"hubs", J.Json.arr(Hubs.rows(ss))}, J.KV{"found", J.Json.arr(Hubs.found(list))}]) @@ -952,4 +1192,4 @@ def Hubs.screen.k(+ss: List<&2, Shown>, +cur: String, +list: List<&2, Hub>) -> J def Hubs.screen(hs: Hubs) -> J.Json: match hs: case Hubs{_, _, +cur, +list}: - Hubs.screen.k(Hubs.relabel(Hubs.shown(list)), cur, list) + Hubs.screen.k(Hubs.relabel(Hubs.catalog.shown(Hubs.shown(list), list)), cur, list) diff --git a/src/mobile/screen.bend b/src/mobile/screen.bend index f943406cf4..2ee411737f 100644 --- a/src/mobile/screen.bend +++ b/src/mobile/screen.bend @@ -83,7 +83,7 @@ def Mob.when(shelf: Nat, +st: M.State, +id: String, +a: Nat, +now: Nat) -> Strin def Mob.row(t: M.Thread, +status: String, +st: M.State, +now: Nat, +shelf: Nat) -> J.Json: match t: case M.Thread{+id, _, title, _, +a, turn, _, _, _, _, _, pinned, _, _, _, _, _, _, _, _, _}: - J.Json.obj([J.KV{"id", J.Json.str(id)}, J.KV{"title", J.Json.str(title)}, J.KV{"state", J.Json.str(Mob.state(turn))}, + J.Json.obj([J.KV{"id", J.Json.str(id)}, J.KV{"title", J.Json.str(U.Label.title(st, id, title))}, J.KV{"state", J.Json.str(Mob.state(turn))}, J.KV{"ago", J.Json.str(Mob.when(shelf, st, id, a, now))}, J.KV{"agents", J.Json.str(SB.Subs.badge(SB.Subs.count(st, id)))}, J.KV{"pinned", J.Json.bool(pinned)}, J.KV{"status", J.Json.str(status)}, J.KV{"lead", J.Json.arr(Mob.lead(shelf, id))}, J.KV{"trail", J.Json.arr(Mob.trail(id))}]) @@ -98,7 +98,7 @@ def Mob.rows(ts: List<&2, M.Thread>, +st: M.State, +now: Nat, +shelf: Nat) -> Li def Mob.project(r: U.SRow, +open: Map<&2, Bool>, +st: M.State, +now: Nat) -> J.Json: match r: case U.SRow{_, M.Project{+id, title, root, _, _}, M.Shelves{pinned, active, +settled, hidden, snoozed}}: - J.Json.obj([J.KV{"id", J.Json.str(id)}, J.KV{"title", J.Json.str(title)}, J.KV{"root", J.Json.str(root)}, + J.Json.obj([J.KV{"id", J.Json.str(id)}, J.KV{"title", J.Json.str(U.Label.title(st, id, title))}, J.KV{"root", J.Json.str(root)}, J.KV{"open", J.Json.bool(U.Flag.get(open, id))}, J.KV{"threads", J.Json.arr(Mob.rows(List.append(&2, M.Thread, pinned, active), st, now, 0n))}, J.KV{"snoozedShelf", J.Json.str("Snoozed")}, @@ -768,7 +768,7 @@ def Mob.thread(u: U.Ui, t: M.Thread, pend: List<&2, String>, vw: J.Json, +atts: match u t: case U.Ui{+st, +sel, +drafts, +live, _, _, +now, _, pa, _, _, _, _, _, +nf, _, _} M.Thread{+id, _, title, _, _, +turn, _, archived, o, +sa, +ua, pinned, _, branch, _, +pr, +mo, _, _, _, _}: +pg = Mob.page(drafts, id, st) - J.Json.obj([J.KV{"id", J.Json.str(id)}, J.KV{"title", J.Json.str(title)}, J.KV{"branch", J.Json.str(branch)}, + J.Json.obj([J.KV{"id", J.Json.str(id)}, J.KV{"title", J.Json.str(U.Label.title(st, id, title))}, J.KV{"branch", J.Json.str(branch)}, J.KV{"state", J.Json.str(Mob.state(turn))}, J.KV{"tools", J.Json.arr(Mob.head(V.View.running(turn), pinned, TO.Tools.settled(o, Nat.is_gt(sa, ua)), archived, Bool.not(String.is_empty(M.Fork.source(st, id)))))}, J.KV{"menu", J.Json.arr(Mob.menu(id, M.Snooze.on(now, U.Side.until(st, id))))}, diff --git a/src/server/botnet.bend b/src/server/botnet.bend index ab9eac8001..8c14a80d1c 100644 --- a/src/server/botnet.bend +++ b/src/server/botnet.bend @@ -7,6 +7,7 @@ import ../core/sign.bend as SG import ../core/model.bend as M import ../core/bot.bend as BO import ../core/bothub.bend as BH +import ../core/far.bend as FR # Bots' IO that needs secrets (docs/bots.md): webhook and peer secrets, # signed requests to linked machines, verifying signed requests to us, and @@ -18,18 +19,29 @@ import ../core/bothub.bend as BH # reads the signed headers on stdin and the body from a 0600 file. # what the server keeps for bots: the linked machines' bots (a JSON array, -# as clients read it) and the signatures accepted within the window +# as clients read it), the signatures accepted within the window, and the +# threads elsewhere (far.bend: mirrors, pushes, this hub's feed) type Bn is Data: - Bn{dir: String, seen: SG.Seen} + Bn{dir: String, seen: SG.Seen, work: BH.DirWork, far: FR.Fh} def Bn.empty() -> Bn: - Bn{"[]", SG.Seen.empty()} + Bn{"[]", SG.Seen.empty(), BH.DirWork.empty(), FR.Fh.empty()} def Bn.dir(b: Bn) -> String: match b: - case Bn{d, _}: + case Bn{d, _, _, _}: d +def Bn.far(b: Bn) -> FR.Fh: + match b: + case Bn{_, _, _, f}: + f + +def Bn.with_far(b: Bn, f: FR.Fh) -> Bn: + match b: + case Bn{d, s, w, _}: + Bn{d, s, w, f} + # Files # ----- @@ -416,18 +428,18 @@ def Bn.seen.k(had: Bool, s: SG.Seen, +key: String, +now: Nat) -> Maybe<&2, SG.Se def Bn.accept(+b: Bn, +key: String, +now: Nat) -> Maybe<&2, SG.Seen>: match b: - case Bn{_, +s}: + case Bn{_, +s, _, _}: Bn.seen.k(SG.Seen.has(s, key), s, key, now) def Bn.with_seen(b: Bn, s: SG.Seen) -> Bn: match b: - case Bn{d, _}: - Bn{d, s} + case Bn{d, _, w, f}: + Bn{d, s, w, f} def Bn.with_dir(b: Bn, d: String) -> Bn: match b: - case Bn{_, s}: - Bn{d, s} + case Bn{_, s, w, f}: + Bn{d, s, w, f} # Signed requests out # ------------------- @@ -575,10 +587,6 @@ def Bn.mine.go(bs: List<&2, BO.Bot>, +s: M.State) -> List<&2, J.Json>: def Bn.mine(+s: M.State) -> String: J.Json.show(J.Json.arr(Bn.mine.go(BO.World.bots(M.State.bots(s)), s))) -def Bn.item(+peer: String, +pname: String, +look: Nat, +name: String, +state: String, +seen: Nat) -> J.Json: - J.Json.obj([J.KV{"name", J.Str{name}}, J.KV{"peer", J.Str{peer}}, J.KV{"peerName", J.Str{pname}}, J.KV{"look", J.Json.nat(look)}, - J.KV{"state", J.Str{state}}, J.KV{"seen", J.Json.nat(seen)}]) - def Bn.item.keep(blank: Bool, it: J.Json, rest: List<&2, J.Json>) -> List<&2, J.Json>: match blank: case True{}: @@ -592,48 +600,76 @@ def Bn.items(xs: List<&2, J.Json>, +peer: String, +pname: String, +now: Nat) -> Nil{} case +o <> t: Bn.item.keep(String.is_empty(Bn.clean(J.Json.get_str(o, "name", ""))), - Bn.item(peer, pname, J.Json.get_nat(o, "look", 0n), Bn.clean(J.Json.get_str(o, "name", "")), Bn.clean(J.Json.get_str(o, "state", "ready")), now), + BH.Dir.item(peer, pname, J.Json.get_nat(o, "look", 0n), Bn.clean(J.Json.get_str(o, "name", "")), Bn.clean(J.Json.get_str(o, "state", "ready")), now), Bn.items(t, peer, pname, now)) -def Bn.away.k(mine: Bool, +o: J.Json, rest: List<&2, J.Json>) -> List<&2, J.Json>: - match mine: +# Only directory requests use the short deadline. Delivery keeps Bn.args +# and its existing 15-second timeout; signed headers still go over stdin. +def Bn.dir.args(url: String) -> List<&2, String>: + ["-sS", "--proto", "=http,https", "--connect-timeout", "2", "-m", "4", "--max-filesize", "1048576", + "-X", "GET", "-H", "@-", "-w", "\n%{http_code}", "--url", url] + +def Bn.dir.signed(ok: Bool, +url: String, +peer: String, +secret: String, +now: Nat) -> IO(Resp): + match ok: case True{}: - Bn.item(J.Json.get_str(o, "peer", ""), J.Json.get_str(o, "peerName", ""), J.Json.get_nat(o, "look", 0n), J.Json.get_str(o, "name", ""), "away", - J.Json.get_nat(o, "seen", 0n)) <> rest + do IO: + r : P.Ran <- Bn.curl(Bn.dir.args(Bn.url.base(url) ++ "/bots/dir"), Bn.headers(peer, now, SG.Sign.header(secret, now, ""))) + return Bn.resp(r) case False{}: - rest + IO.pure(Resp, Resp{0n, ""}) -# a machine that did not answer keeps the bots it had, away -def Bn.away(xs: List<&2, J.Json>, +peer: String) -> List<&2, J.Json>: - match xs: - case Nil{}: - Nil{} - case +o <> t: - Bn.away.k(String.eq(J.Json.get_str(o, "peer", ""), peer), o, Bn.away(t, peer)) +def Bn.dir.fetch(+home: String, +url: String, +peer: String, +now: Nat) -> IO(Resp): + do IO: + +secret : String <- Bn.peer.secret(home, peer) + Bn.dir.signed(Bool.and(Bn.url.ok(url), Bool.not(String.is_empty(secret))), url, peer, secret, now) -def Bn.fetched.j(m: Maybe<&2, J.Json>, +peer: String, +pname: String, +now: Nat, +prev: List<&2, J.Json>) -> List<&2, J.Json>: +# Malformed JSON, including a valid object rather than an array, fails. +# An empty array is a successful replacement (the peer removed its bots). +def Bn.fetched.j(m: Maybe<&2, J.Json>, +peer: String, +pname: String, +now: Nat) -> Maybe<&2, List<&2, J.Json>>: match m: - case Some{+j}: - Bn.items(List.take(&2, J.Json, J.Json.elems(j), 200n), peer, pname, now) - case None{}: - Bn.away(prev, peer) + case Some{J.Arr{xs}}: + Some{Bn.items(List.take(&2, J.Json, J.Json.elems(J.Arr{xs}), 200n), peer, pname, now)} + case _: + None{} -def Bn.fetched(r: Resp, +peer: String, +pname: String, +now: Nat, +prev: List<&2, J.Json>) -> List<&2, J.Json>: +def Bn.fetched(r: Resp, +peer: String, +pname: String, +now: Nat) -> Maybe<&2, List<&2, J.Json>>: match r: - case Resp{+code, +body}: - Bn.fetched.j(Bool.pick(Maybe<&2, J.Json>, Nat.is_eq(code, 200n), J.Json.parse(body), None{}), peer, pname, now, prev) + case Resp{200n, body}: + Bn.fetched.j(J.Json.parse(body), peer, pname, now) + case _: + None{} -def Bn.dir.go(ps: List<&2, BO.Peer>, +home: String, +now: Nat, +prev: List<&2, J.Json>, acc: List<&2, J.Json>) -> IO(List<&2, J.Json>): - match ps: - case Nil{}: - IO.pure(List<&2, J.Json>, acc) - case BO.Peer{+i, +n, +u, _, _} <> t: - do IO>: - r : Resp <- Bn.to_peer(home, u, i, now, "/bots/dir", False{}, "") - Bn.dir.go(t, home, now, prev, List.append(&2, J.Json, acc, Bn.fetched(r, i, Bool.pick(String, String.is_empty(n), i, n), now, prev))) - -# every linked machine's bots (asked GET /bots/dir, signed), as the JSON -# array clients read: [{"name", "peer", "peerName", "look", "state", "seen"}] -def Bn.dir.fetch(ps: List<&2, BO.Peer>, +home: String, +now: Nat, +prev: String) -> IO(String): - IO.bind(List<&2, J.Json>, String, Bn.dir.go(ps, home, now, J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse(prev), J.Null{})), Nil{}), - xs => IO.pure(String, J.Json.show(J.Json.arr(xs)))) +def Bn.work(b: Bn) -> BH.DirWork: + match b: + case Bn{_, _, w, _}: + w + +def Bn.with_work(b: Bn, w: BH.DirWork) -> Bn: + match b: + case Bn{d, s, _, f}: + Bn{d, s, w, f} + +def Bn.cache(home: String) -> String: + home ++ "/bots.cache" + +# One snapshot holds the last directory per peer, with original `seen`. +# The hub serializes these small local writes; rename keeps a restart +# from reading a partial file. No event-log entries or secrets here. +def Bn.cache.saved(ok: Bool, +home: String) -> IO(Unit): + match ok: + case True{}: + do IO: + r : Result<&1, &1, U32 & String, Unit> <- H.FS.rename(Bn.cache(home) ++ ".tmp", Bn.cache(home)) + return Unit{} + case False{}: + IO.pure(Unit, Unit{}) + +def Bn.cache.save(+home: String, text: String) -> IO(Unit): + IO.bind(Bool, Unit, Bf.secret(Bn.cache(home) ++ ".tmp", text), ok => Bn.cache.saved(ok, home)) + +def Bn.cache.load(text: String, ps: List<&2, BO.Peer>) -> String: + J.Json.show(J.Json.arr(BH.Dir.cached(J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse(text), J.Null{})), ps))) + +# A mirror batch appended to its file (made when missing). +def Bf.append(+path: String, text: String) -> IO(Bool): + IO.bind(Result<&1, &1, U32 & String, File>, Bool, File.open(path, "a"), r => Bf.write.opened(r, text)) diff --git a/src/server/server.bend b/src/server/server.bend index 82617a1832..a8f5b1b43c 100644 --- a/src/server/server.bend +++ b/src/server/server.bend @@ -31,6 +31,8 @@ import ../core/sha1.bend as SH import ../core/boot.bend as BT import ../core/store.bend as ST import ./botnet.bend as BN +import ../core/far.bend as FR +import ../core/client.bend as UC import ../core/bothub.bend as BH import ../core/bot.bend as BO import ../core/space.bend as SPC @@ -100,7 +102,9 @@ type Msg is Data: # this machine's UTC offset as `date +%z` prints it BotTick{tz: String} # the bots on linked machines (a JSON array) - BotsFound{dir: String} + BotsFound{peer: String, url: String, result: Maybe<&2, List<&2, J.Json>>} + # one peer, or every peer when empty; the hub coalesces active fetches + BotsRefresh{peer: String} # a request for bots' routes (a webhook, a linked machine, a bot's # screen, Google's sign-in redirect); the reply is the whole response # (`ct` its content type; `raw` its bytes, kept only for a form with a @@ -116,6 +120,18 @@ type Msg is Data: NewProject{thread: String, root: String, name: String, id: String, reply: Chan(String)} # a phone's upload (POST /attach) asks where a thread's attachments go AttDir{thread: String, reply: Chan(String)} + # threads elsewhere (far.bend): this hub's feed made from its log; a + # push ended; a page pulled (code 0: no answer); a request sent on for + # a client answered; a mirror read back from disk; the linked machines + # that answered the minute's directory (one id a line) + FarFeed{lines: List<&2, FR.Line>, epoch: String} + FarPushed{link: String, ok: Bool, to: Nat} + FarPulled{link: String, since: Nat, code: Nat, body: String} + FarReplied{client: U32, id: String, link: String, code: Nat, body: String} + FarLoaded{link: String, name: String, text: String} + FarHere{answered: String} + # a pull to try again (the other hub was still making its feed) + FarRetry{link: String} # remote access: also listen on the tailnet, serve HTTPS through # `tailscale serve`, and the token remote clients must present @@ -624,17 +640,30 @@ def Hub.info(c: Cfg) -> J.Json: case Cfg{+home, _, host, port, _, _, _, net}: J.Json.obj(List.append(&2, J.KV, [J.KV{"home", J.Str{home}}, J.KV{"browser.frames", J.Str{home ++ "/browser.rgb"}}, J.KV{"listen", J.Str{host ++ ":" ++ U32.show(port)}}], Info.net(net))) -def Hub.commit.wrote(r: File & Result<&1, &1, U32 & String, Unit>, st: M.State, +line: String, ls: List<&2, String>, cl: List<&2, Client>, +# A link change is handled after it has been stored, through the inbox, +# so both sides of an invite refresh immediately (revokes prune rows). +def Bot.dir.changed(c: M.Change, +inbox: Chan(Msg)) -> IO(Unit): + match c: + case M.PeerSet{peer, _, _, _, _}: + do IO: + ok : Bool <- Chan.send(Msg, inbox, BotsRefresh{peer}) + return Unit{} + case _: + IO.pure(Unit, Unit{}) + +def Hub.commit.wrote(r: File & Result<&1, &1, U32 & String, Unit>, c: M.Change, st: M.State, +line: String, ls: List<&2, String>, cl: List<&2, Client>, as: List<&2, Agent>, +inbox: Chan(Msg), cfg: Cfg, note: String, fresh: List<&2, String>, k: Hub -> List<&2, String> -> IO(Hub & List<&2, String>)) -> IO(Hub & List<&2, String>): (f, _) = r - k(Hub{st, f, line <> ls, cl, as, inbox, cfg, note}, line <> fresh) + do IO>: + Bot.dir.changed(c, inbox) + k(Hub{st, f, line <> ls, cl, as, inbox, cfg, note}, line <> fresh) # the full line goes to disk; `wire`, its copy for clients, to the log -def Hub.commit.one(h: Hub, line: String, +wire: String, c: M.Change, fresh: List<&2, String>, k: Hub -> List<&2, String> -> IO(Hub & List<&2, String>)) -> IO(Hub & List<&2, String>): +def Hub.commit.one(h: Hub, line: String, +wire: String, +c: M.Change, fresh: List<&2, String>, k: Hub -> List<&2, String> -> IO(Hub & List<&2, String>)) -> IO(Hub & List<&2, String>): match h: case Hub{st, f, ls, cl, as, +inbox, cfg, note}: IO.bind(File & Result<&1, &1, U32 & String, Unit>, Hub & List<&2, String>, File.write(f, line ++ "\n"), - r => Hub.commit.wrote(r, M.State.apply(st, c), wire, ls, cl, as, inbox, cfg, note, fresh, k)) + r => Hub.commit.wrote(r, c, M.State.apply(st, c), wire, ls, cl, as, inbox, cfg, note, fresh, k)) def Hub.announce(r: Hub & List<&2, String>) -> IO(Hub): (h, fresh) = r @@ -745,8 +774,6 @@ def Hub.join.note.k(empty: Bool, +out: Chan(Wire), +note: String) -> IO(Unit): def Hub.join.note(+out: Chan(Wire), +note: String) -> IO(Unit): Hub.join.note.k(String.is_empty(note), out, note) -# the window in this process: the read model (devices left out, as the -# wire leaves them) and the log message with no items # Desks # ----- # which threads a desk watches (core/desk.bend): a client joining hears @@ -775,24 +802,6 @@ def Hub.desk(h: Hub, +client: U32, +thread: String, +focused: Bool) -> IO(Hub): Desk.say(DK.Desk.text(Clients.looks(cl), now), cl2, now) return Hub{st, f, ls, cl2, as, inbox, cfg, note} -def Hub.join.here(h: Hub, +client: U32, +out: Chan(Wire), +now: Nat) -> IO(Hub): - match h: - case Hub{+st, f, +ls, +cl, as, inbox, +cfg, +note}: - do IO: - ok : Bool <- Chan.send(Wire, out, WState{M.State.private(st), Hub.log_msg.k(0n, ls, cfg, 0n)}) - Hub.join.note(out, note) - Hub.join.note(out, Desk.join(DK.Desk.text(Clients.looks(cl), now))) - return Hub{st, f, ls, Client{client, out, DK.Look.none()} <> cl, as, inbox, cfg, note} - -def Hub.join(h: Hub, +client: U32, +out: Chan(Wire), +since: Nat, +origin: String, +now: Nat) -> IO(Hub): - match h: - case Hub{st, f, +ls, +cl, as, inbox, +cfg, +note}: - do IO: - ok : Bool <- Chan.send(Wire, out, WText{Hub.log_msg(ls, cfg, Bool.pick(Nat, String.eq(origin, Hub.origin(ls)), since, 0n))}) - Hub.join.note(out, note) - Hub.join.note(out, Desk.join(DK.Desk.text(Clients.looks(cl), now))) - return Hub{st, f, ls, Client{client, out, DK.Look.none()} <> cl, as, inbox, cfg, note} - def Hub.leave.close(m: Maybe<&2, Chan(Wire)>) -> IO(Unit): match m: case Some{c}: @@ -3394,6 +3403,11 @@ def Hub.turn_done(h: Hub, +tag: String) -> IO(Hub): # Google's tools, a bot's own browser page. Secrets are handled by # botnet.bend and never reach the log, the event log or a broadcast. +def Cfg.machines(c: Cfg) -> String: + match c: + case Cfg{_, _, _, _, _, _, _, Net{_, _, _, _, _, _, _, _, _, mc, _, _, _, _, _}}: + mc + def Cfg.bn(c: Cfg) -> BN.Bn: match c: case Cfg{_, _, _, _, _, _, _, Net{_, _, _, _, _, _, brx, pkx, tmx, mc, pvx, bnx, mgd, lk, fc}}: @@ -3980,13 +3994,596 @@ def Hub.outs(os: List<&2, B.Out>, h: Hub) -> IO(Hub): case o <> t: IO.bind(Hub, Hub, Hub.out(h, o), h2 => Hub.outs(t, h2)) -def Hub.step.k(p: M.State & Hub, cs: List<&2, M.Change>, os: List<&2, B.Out>) -> IO(Hub): - (st, h) = p +# Threads elsewhere (far.bend, docs/bots.md) +# ------------------------------------------ +# This hub shares its bots and their threads with the owner's other +# machines it is linked to: each commit's shared changes are pushed to +# each (POST /far/push, one push out per link at a time), and a machine +# that missed some pulls them (POST /far/log, paged). What the others +# share with this one is kept as a mirror per link (in memory and in +# /far/.jsonl) and handed to every client ({"t":"far"}); a +# client's request about something elsewhere goes to its hub (POST +# /far/rpc). Every request between hubs is signed like /bots/deliver. The +# network is only ever used off the hub, in jobs that answer by message. + +def Far.fh(h: Hub) -> FR.Fh & Hub: + match h: + case Hub{st, f, ls, cl, as, inbox, +cfg, note}: + (BN.Bn.far(Cfg.bn(cfg)), Hub{st, f, ls, cl, as, inbox, cfg, note}) + +def Far.with(h: Hub, fh: FR.Fh) -> Hub: + match h: + case Hub{st, f, ls, cl, as, inbox, +cfg, note}: + Hub{st, f, ls, cl, as, inbox, Cfg.with_bn(cfg, BN.Bn.with_far(Cfg.bn(cfg), fh)), note} + +# the machines' list (self and peers, as the switcher shows it) +def Far.machines(+cfg: Cfg) -> J.Json: + Maybe.default(&2, J.Json, J.Json.parse(Cfg.machines(cfg)), J.Null{}) + +# whether a linked machine is one of the owner's own (only those get this +# hub's threads, or may act on them) +def Far.some(m: Maybe<&2, String>) -> Bool: + match m: + case Some{_}: + True{} + case None{}: + False{} + +def Far.own(+cfg: Cfg, p: BO.Peer) -> Bool: + match p: + case BO.Peer{_, +n, +u, _, r}: + Bool.and(Bool.not(r), Far.some(UC.Far.machine(Far.machines(cfg), n, u))) + +def Far.keep(ok: Bool, p: BO.Peer, rest: List<&2, BO.Peer>) -> List<&2, BO.Peer>: + match ok: + case True{}: + p <> rest + case False{}: + rest + +def Far.own.go(ps: List<&2, BO.Peer>, +cfg: Cfg) -> List<&2, BO.Peer>: + match ps: + case Nil{}: + Nil{} + case +p <> t: + Far.keep(Far.own(cfg, p), p, Far.own.go(t, cfg)) + +def Far.own.id(+st: M.State, +cfg: Cfg, +id: String) -> Bool: + Bool.and(Bot.peer.live(st, id), Far.own(cfg, BO.World.peer(M.State.bots(st), id))) + +def Far.dir(+home: String) -> String: + home ++ "/far" + +def Far.file(+home: String, +link: String) -> String: + Far.dir(home) ++ "/" ++ link ++ ".jsonl" + +# Jobs (off the hub) +# ------------------ + +def Far.send(+inbox: Chan(Msg), m: Msg) -> IO(Unit): + do IO: + ok : Bool <- Chan.send(Msg, inbox, m) + return Unit{} + +def Far.code(r: BN.Resp) -> Nat: + match r: + case BN.Resp{c, _}: + c + +def Far.body(r: BN.Resp) -> String: + match r: + case BN.Resp{_, b}: + b + +# a push too big for a request says only where it ends, past a gap, so +# the other end pulls it in pages +# The first durable change identifies the history. Cache its hash at +# startup or the first (small) commit, before accepting new pushes. +def Far.history(+ls: List<&2, String>) -> String: + Bool.pick(String, List.is_empty(&2, String, ls), "", SG.Sign.header("00", 0n, Maybe.default(&2, String, List.last(&2, String, ls), ""))) + +def Far.push.body(+s: FR.Send, +name: String) -> String: + Bool.pick(String, Nat.is_gt(String.length(FR.Send.body(s, name)), 200000n), FR.Send.body(FR.Send.gap(s), name), FR.Send.body(s, name)) + +def Far.push.job(s: FR.Send, +epoch: String, +home: String, +url: String, +inbox: Chan(Msg)) -> IO(Unit): + match s: + case FR.Send{+link, +since, +to, xs}: + do IO: + now : Nat <- Now.nat() + +name : String <- BN.Bn.name() + +r : BN.Resp <- BN.Bn.to_peer(home, url, link, now, "/far/push", True{}, FR.Fr.with.epoch(Far.push.body(FR.Send{link, since, to, xs}, name), epoch)) + Far.send(inbox, FarPushed{link, Nat.is_eq(Far.code(r), 200n), to}) + +def Far.pull.job(+link: String, +since: Nat, +epoch: String, +home: String, +url: String, +inbox: Chan(Msg)) -> IO(Unit): + do IO: + now : Nat <- Now.nat() + +r : BN.Resp <- BN.Bn.to_peer(home, url, link, now, "/far/log", True{}, J.Json.show(J.Json.obj([J.KV{"since", J.Json.nat(since)}, J.KV{"epoch", J.Str{epoch}}]))) + Far.send(inbox, FarPulled{link, since, Far.code(r), Far.body(r)}) + +def Far.rpc.job(+client: U32, +id: String, +link: String, +body: String, +home: String, +url: String, +inbox: Chan(Msg)) -> IO(Unit): + do IO: + now : Nat <- Now.nat() + +r : BN.Resp <- BN.Bn.to_peer(home, url, link, now, "/far/rpc", True{}, body) + Far.send(inbox, FarReplied{client, id, link, Far.code(r), Far.body(r)}) + +# this hub's shared lines, made from its log (newest first) +def Far.feed.job(+ls: List<&2, String>, +st: M.State, +inbox: Chan(Msg)) -> IO(Unit): + Far.send(inbox, FarFeed{FR.Fr.feed(ls, st), ""}) + +# a mirror kept on disk, read back +def Far.load.job(+link: String, +name: String, +home: String, +inbox: Chan(Msg)) -> IO(Unit): + do IO: + +text : String <- Files.text(Far.file(home, link)) + Far.send(inbox, FarLoaded{link, name, text}) + +def Far.keep.job(+path: String, +text: String) -> IO(Unit): + do IO: + ok : Bool <- BN.Bf.append(path, text) + return Unit{} + +def Far.out.put.k(p: FR.Fh & Hub, o: FR.Out) -> IO(Hub): + (fh, h) = p + IO.pure(Hub, Far.with(h, FR.Fh.out.set(fh, o))) + +def Far.out.put(h: Hub, o: FR.Out) -> IO(Hub): + Far.out.put.k(Far.fh(h), o) + +def Far.push.next.k(m: Maybe<&2, FR.Send>, o: FR.Out, h: Hub, +url: String) -> IO(Hub): + match m h: + case Some{s} Hub{st, f, ls, cl, as, +inbox, +cfg, note}: + do IO: + IO.spawn(Unit, Far.push.job(s, FR.Fh.epoch(BN.Bn.far(Cfg.bn(cfg))), Cfg.home(cfg), url, inbox)) + Far.out.put(Hub{st, f, ls, cl, as, inbox, cfg, note}, o) + case None{} _: + Far.out.put(h, o) + +# Pushing +# ------- + +def Far.push.next(r: Maybe<&2, FR.Send> & FR.Out, h: Hub, +url: String) -> IO(Hub): + (m, o) = r + Far.push.next.k(m, o, h, url) + +def Far.out.of(p: FR.Fh & Hub, +link: String) -> FR.Out & Hub: + (fh, h) = p + (FR.Fh.out(fh, link), h) + +def Far.push.one.k(r: FR.Out & Hub, +url: String, +from: Nat, +to: Nat, +xs: List<&2, FR.Item>) -> IO(Hub): + (o, h) = r + Far.push.next(FR.Out.next(FR.Out.add(o, from, to, xs)), h, url) + +def Far.push.go(ps: List<&2, BO.Peer>, h: Hub, +from: Nat, +to: Nat, +xs: List<&2, FR.Item>) -> IO(Hub): + match ps: + case Nil{}: + IO.pure(Hub, h) + case BO.Peer{+i, _, +u, _, _} <> t: + IO.bind(Hub, Hub, Far.push.one.k(Far.out.of(Far.fh(h), i), u, from, to, xs), h2 => Far.push.go(t, h2, from, to, xs)) + +# When discovery first recognizes a linked machine as the owner's, offer +# our current head. Its mirror can pull even if our commits happened +# before discovery, or our hub restarted without an observed away tick. +# Only discovery's newly returned peers are offered; no extra timer. +def Far.offer.job(+link: String, +head: Nat, +epoch: String, +home: String, +url: String) -> IO(Unit): + do IO: + now : Nat <- Now.nat() + +name : String <- BN.Bn.name() + r : BN.Resp <- BN.Bn.to_peer(home, url, link, now, "/far/push", True{}, + FR.Fr.with.epoch(FR.Send.body(FR.Send{link, head, head, Nil{}}, name), epoch)) + return Unit{} + +# An empty head hint is deliberately outside the item queue: Out.next +# sends only batches with items. It carries no acknowledgement for that +# queue, and cannot reset a mirror without an authoritative pull. +def Far.offer.go(ps: List<&2, BO.Peer>, +head: Nat, +epoch: String, +home: String) -> IO(Unit): + match ps: + case Nil{}: + IO.pure(Unit, Unit{}) + case BO.Peer{+link, _, +url, _, _} <> t: + do IO: + IO.spawn(Unit, Far.offer.job(link, head, epoch, home, url)) + Far.offer.go(t, head, epoch, home) + +def Far.offer(h: Hub, +ps: List<&2, BO.Peer>) -> IO(Hub): + match h: + case Hub{+st, f, ls, cl, as, inbox, +cfg, note}: + do IO: + Far.offer.go(Far.own.go(ps, cfg), M.State.seq(st), FR.Fh.epoch(BN.Bn.far(Cfg.bn(cfg))), Cfg.home(cfg)) + return Hub{st, f, ls, cl, as, inbox, cfg, note} + +def Far.feed.put(p: FR.Fh & Hub, ls: List<&2, FR.Line>) -> Hub: + (fh, h) = p + Far.with(h, FR.Fh.feed.add(fh, ls)) + +def Far.push.k(empty: Bool, h: Hub, +lines: List<&2, FR.Line>, +from: Nat, +to: Nat) -> IO(Hub): + match empty h: + case True{} _: + IO.pure(Hub, h) + case False{} Hub{+st, f, ls, cl, as, inbox, +cfg, note}: + Far.push.go(Far.own.go(BO.World.peers(M.State.bots(st)), cfg), Far.feed.put(Far.fh(Hub{st, f, ls, cl, as, inbox, cfg, note}), lines), from, to, + FR.Fr.lines.items(lines, Nil{})) + +def Far.push.lines(+lines: List<&2, FR.Line>, h: Hub, +from: Nat, +to: Nat) -> IO(Hub): + Far.push.k(List.is_empty(&2, FR.Line, lines), h, lines, from, to) + +# a commit took the log from `from`: its shared changes join the feed and +# go to each of the owner's linked machines +def Far.history.first.k(first: Bool, fh: FR.Fh, h: Hub, +fresh: List<&2, String>) -> Hub: + match first: + case True{}: + Far.with(h, FR.Fh.epoch.set(fh, Far.history(fresh))) + case False{}: + Far.with(h, fh) + +def Far.history.first(first: Bool, p: FR.Fh & Hub, +fresh: List<&2, String>) -> Hub: + (fh, h) = p + Far.history.first.k(first, fh, h, fresh) + +def Far.push(h: Hub, cs: List<&2, M.Change>, +fresh: List<&2, String>, +from: Nat) -> IO(Hub): + match h: + case Hub{+st, f, ls, cl, as, inbox, cfg, note}: + Far.push.lines(FR.Fr.feed.add(cs, List.reverse(&2, String, fresh), from, st, Nil{}), Far.history.first(Nat.is_eq(from, 0n), Far.fh(Hub{st, f, ls, cl, as, inbox, cfg, note}), fresh), from, M.State.seq(st)) + +def Far.pushed.go(+o: FR.Out, h: Hub, ok: Bool, +to: Nat) -> IO(Hub): + match h: + case Hub{+st, f, ls, cl, as, inbox, cfg, note}: + Far.push.next(FR.Out.next(FR.Out.done(o, ok, to)), Hub{st, f, ls, cl, as, inbox, cfg, note}, Bot.peer.url(st, FR.Out.link(o))) + +def Far.pushed.k(r: FR.Out & Hub, ok: Bool, +to: Nat) -> IO(Hub): + (o, h) = r + Far.pushed.go(o, h, ok, to) + +# a push ended: the next waiting one goes +def Hub.far.pushed(h: Hub, +link: String, ok: Bool, +to: Nat) -> IO(Hub): + Far.pushed.k(Far.out.of(Far.fh(h), link), ok, to) + +# Mirrors +# ------- + +def Far.in.of(p: FR.Fh & Hub, +link: String, +name: String) -> FR.In & Hub: + (fh, h) = p + (FR.Fh.in(fh, link, name), h) + +def Far.in.put.k(p: FR.Fh & Hub, i: FR.In) -> Hub: + (fh, h) = p + Far.with(h, FR.Fh.in.set(fh, i)) + +def Far.in.put(h: Hub, i: FR.In) -> Hub: + Far.in.put.k(Far.fh(h), i) + +# every client hears a mirror's news +def Far.tell(h: Hub, +text: String) -> IO(Hub): + match h: + case Hub{st, f, ls, cl, as, inbox, +cfg, note}: + Hub.broadcast(Hub{st, f, ls, cl, as, inbox, cfg, note}, FR.Fr.with.epoch(text, + FR.In.epoch(FR.Fh.in(BN.Bn.far(Cfg.bn(cfg)), J.Json.get_str(Maybe.default(&2, J.Json, J.Json.parse(text), J.Null{}), "link", ""), "")))) + +def Far.pull.go(busy: Bool, +fh: FR.Fh, h: Hub, +link: String, +since: Nat) -> IO(Hub): + match busy h: + case True{} _: + IO.pure(Hub, Far.with(h, fh)) + case False{} Hub{+st, f, ls, cl, as, +inbox, +cfg, note}: + do IO: + IO.spawn(Unit, Far.pull.job(link, since, FR.In.epoch(FR.Fh.in(fh, link, link)), Cfg.home(cfg), Bot.peer.url(st, link), inbox)) + return Far.with(Hub{st, f, ls, cl, as, inbox, cfg, note}, FR.Fh.pull.on(fh, link)) + +def Far.pull.f(+fh: FR.Fh, h: Hub, +link: String, +since: Nat) -> IO(Hub): + Far.pull.go(FR.Fh.pulling(fh, link), fh, h, link, since) + +# a pull, unless one is out for the link +def Far.pull.p(p: FR.Fh & Hub, +link: String, +since: Nat) -> IO(Hub): + (fh, h) = p + Far.pull.f(fh, h, link, since) + +def Far.pull(h: Hub, +link: String, +since: Nat) -> IO(Hub): + Far.pull.p(Far.fh(h), link, since) + +def Far.mirror.head.k(p: FR.Fh & Hub, +link: String) -> Nat & Hub: + (+fh, h) = p + (FR.In.head(FR.Fh.in(fh, link, link)), Far.with(h, fh)) + +def Far.mirror.head(h: Hub, +link: String) -> Nat & Hub: + Far.mirror.head.k(Far.fh(h), link) + +def Far.pull.here.k(r: Nat & Hub, +link: String) -> IO(Hub): + (hd, h) = r + Far.pull(h, link, hd) + +# a pull from where the mirror is +def Far.pull.here(h: Hub, +link: String) -> IO(Hub): + Far.pull.here.k(Far.mirror.head(h, link), link) + +# Recognition must pull too: the other side's earlier hint may have +# arrived before this side recognized ownership and been refused. Pull +# even a healthy cached mirror, so a quick history replacement is found. +def Far.returned.go(ps: List<&2, BO.Peer>, h: Hub) -> IO(Hub): + match ps: + case Nil{}: + IO.pure(Hub, h) + case BO.Peer{+link, _, _, _, _} <> t: + IO.bind(Hub, Hub, Far.pull.here(h, link), h2 => Far.returned.go(t, h2)) + +def Far.returned(h: Hub, +ps: List<&2, BO.Peer>) -> IO(Hub): + match h: + case Hub{st, f, ls, cl, as, inbox, +cfg, note}: + IO.bind(Hub, Hub, Far.offer(Hub{st, f, ls, cl, as, inbox, cfg, note}, ps), + h2 => Far.returned.go(Far.own.go(ps, cfg), h2)) + +def Far.store(h: Hub, +link: String, +text: String) -> IO(Hub): + match h: + case Hub{st, f, ls, cl, as, inbox, +cfg, note}: + do IO: + BN.Bf.dir(Far.dir(Cfg.home(cfg))) + Far.keep.job(Far.file(Cfg.home(cfg), link), text) + return Hub{st, f, ls, cl, as, inbox, cfg, note} + +def Far.took.go(none: Bool, moved: Bool, h: Hub, +link: String, +from: Nat, +hd: Nat, +ys: List<&2, FR.Item>, +name: String) -> IO(Hub): + match none moved: + case True{} False{}: + IO.pure(Hub, h) + case True{} True{}: + Far.tell(h, FR.Fr.msg(link, name, False{}, from, hd, Nil{})) + case False{} _: + IO.bind(Hub, Hub, Far.store(h, link, FR.Fr.file.batch(ys, hd)), h2 => Far.tell(h2, FR.Fr.msg(link, name, False{}, from, hd, ys))) + +# a batch taken into the mirror: kept, told, and on a gap the rest pulled +def Far.took.k(g: FR.Got, +i: FR.In, h: Hub, +since: Nat, +to: Nat, +xs: List<&2, FR.Item>, +name: String, moved: Bool) -> IO(Hub): + match g: + case FR.Got{+_, +_, True{}}: + Far.pull(Far.in.put(h, FR.In.name.set(i, name)), FR.In.link(i), FR.In.head(i)) + case FR.Got{+hd, ys, False{}}: + Far.took.go(List.is_empty(&2, FR.Item, ys), moved, Far.in.put(h, FR.In.name.set(FR.In.put(i, since, to, xs), name)), FR.In.link(i), FR.In.head(i), hd, ys, name) + +def Far.take.i(+i: FR.In, h: Hub, +since: Nat, +to: Nat, +xs: List<&2, FR.Item>, +name: String) -> IO(Hub): + Far.took.k(FR.In.got(i, since, to, xs), i, h, since, to, xs, Bool.pick(String, String.is_empty(name), FR.In.name.of(i), name), FR.In.away(i)) + +def Far.take.p(r: FR.In & Hub, +since: Nat, +to: Nat, +xs: List<&2, FR.Item>, +name: String) -> IO(Hub): + (i, h) = r + Far.take.i(i, h, since, to, xs, name) + +# a batch from a link (a push, or a page pulled) +def Far.take(h: Hub, +link: String, +since: Nat, +to: Nat, +xs: List<&2, FR.Item>, +name: String) -> IO(Hub): + Far.take.p(Far.in.of(Far.fh(h), link, name), since, to, xs, name) + +def Far.reset.put(+i: FR.In, h: Hub) -> IO(Hub): + match h: + case Hub{st, f, ls, cl, as, inbox, +cfg, note}: + do IO: + BN.Bf.dir(Far.dir(Cfg.home(cfg))) + ok : Bool <- BN.Bf.secret(Far.file(Cfg.home(cfg), FR.In.link(i)), J.Json.show(J.Json.obj([J.KV{"epoch", J.Str{FR.In.epoch(i)}}])) ++ "\n") + Far.tell(Far.in.put(Hub{st, f, ls, cl, as, inbox, cfg, note}, i), FR.In.msg(i)) + +def Far.reset.k(reset: Bool, +i: FR.In, h: Hub, +epoch: String) -> IO(Hub): + match reset: + case True{}: + Far.reset.put(FR.In.epoch.set(FR.In.new(FR.In.link(i), FR.In.name.of(i)), epoch), h) + case False{}: + IO.pure(Hub, h) + +def Far.reset.in(r: FR.In & Hub, +epoch: String) -> IO(Hub): + (+i, h) = r + Far.reset.k(FR.Fr.epoch.changed(FR.In.epoch(i), epoch), i, h, epoch) + +def Far.take.epoch(h: Hub, +link: String, +epoch: String, +since: Nat, +to: Nat, +xs: List<&2, FR.Item>, +name: String) -> IO(Hub): + IO.bind(Hub, Hub, Far.reset.in(Far.in.of(Far.fh(h), link, name), epoch), + h2 => Far.take(h2, link, since, to, xs, name)) + +def Far.away.go(was: Bool, +i: FR.In, h: Hub) -> IO(Hub): + match was: + case True{}: + IO.pure(Hub, Far.in.put(h, i)) + case False{}: + Far.tell(Far.in.put(h, FR.In.away.set(i, True{})), FR.Fr.msg(FR.In.link(i), FR.In.name.of(i), True{}, FR.In.head(i), FR.In.head(i), Nil{})) + +def Far.away.i(+i: FR.In, h: Hub) -> IO(Hub): + Far.away.go(FR.In.away(i), i, h) + +# a link that does not answer: its mirror shows, away +def Far.away.p(r: FR.In & Hub) -> IO(Hub): + (i, h) = r + Far.away.i(i, h) + +def Far.away(h: Hub, +link: String) -> IO(Hub): + Far.away.p(Far.in.of(Far.fh(h), link, link)) + +def Far.feed.of(p: FR.Fh & Hub) -> Maybe<&2, List<&2, FR.Line>> & Hub: + (+fh, h) = p + (FR.Fh.feed(fh), Far.with(h, fh)) + +# the client a linked machine's request acts as: none (its answer goes +# back over HTTP, not to a client) +def Far.client() -> U32: + 4000000000 + +def Far.more(more: Bool, h: Hub, +link: String) -> IO(Hub): + match more: + case True{}: + Far.pull.here(h, link) + case False{}: + IO.pure(Hub, h) + +# Messages back from the jobs +# --------------------------- + +def Far.page.k(ok: Bool, m: Maybe<&2, J.Json>, h: Hub, +link: String, +since: Nat) -> IO(Hub): + match ok m: + case True{} Some{+o}: + IO.bind(Hub, Hub, Far.take.epoch(h, link, J.Json.get_str(o, "epoch", ""), J.Json.get_nat(o, "since", since), J.Json.get_nat(o, "head", since), FR.Fr.items.of(J.Json.elems(J.Json.get_obj(o, "items"))), ""), + h2 => Far.more(J.Json.get_bool(o, "more", False{}), h2, link)) + case _ _: + Far.away(h, link) + +def Far.pull.done(p: FR.Fh & Hub, +link: String) -> Hub: + (fh, h) = p + Far.with(h, FR.Fh.pull.off(fh, link)) + +# a page came back (or the pull failed: the link is away until it answers) +def Far.retry.job(+link: String, +inbox: Chan(Msg)) -> IO(Unit): + do IO: + IO.sleep(2000) + Far.send(inbox, FarRetry{link}) + +def Far.pulled.k(busy: Bool, h: Hub, +link: String, +since: Nat, +code: Nat, +body: String) -> IO(Hub): + match busy h: + case True{} Hub{st, f, ls, cl, as, +inbox, cfg, note}: + do IO: + IO.spawn(Unit, Far.retry.job(link, inbox)) + return Hub{st, f, ls, cl, as, inbox, cfg, note} + case False{} _: + Far.page.k(Nat.is_eq(code, 200n), J.Json.parse(body), h, link, since) + +# a page came back (or the pull failed: the link is away until it answers; +# a hub still making its feed is asked again in a moment) +def Hub.far.pulled(h: Hub, +link: String, +since: Nat, +code: Nat, +body: String) -> IO(Hub): + Far.pulled.k(Nat.is_eq(code, 503n), Far.pull.done(Far.fh(h), link), link, since, code, body) + +# a request sent on for a client came back: its answer goes to the client +def Hub.far.replied(h: Hub, +client: U32, +id: String, +link: String, +code: Nat, +body: String) -> IO(Hub): + Hub.push(h, client, Bool.pick(String, Nat.is_eq(code, 200n), FR.Fr.reply(Maybe.default(&2, J.Json, J.Json.parse(body), J.Json.obj([J.KV{"t", J.Str{"reply"}}, J.KV{"ok", J.Flag{False{}}}, J.KV{"error", J.Str{"invalid reply from that machine"}}, J.KV{"id", J.Num{"0"}}])), id), + J.Json.show(J.Json.obj([J.KV{"t", J.Str{"reply"}}, J.KV{"id", J.Num{id}}, J.KV{"ok", J.Flag{False{}}}, + J.KV{"error", J.Str{Bool.pick(String, Nat.is_eq(code, 0n), "that machine is away", "that machine did not take it: " ++ String.take(body, 120n))}}])))) + +def Far.fed.k(p: FR.Fh & Hub, ls: List<&2, FR.Line>, +epoch: String) -> Hub: + (fh, h) = p + Far.with(h, FR.Fh.epoch.set(FR.Fh.feed.ready(fh, ls), epoch)) + +def Hub.far.fed(h: Hub, ls: List<&2, FR.Line>, +epoch: String) -> IO(Hub): + IO.pure(Hub, Far.fed.k(Far.fh(h), ls, epoch)) + +# a mirror read back from disk: shown (away) until its machine answers, +# then pulled from where it ends +def Far.loaded.put(+i: FR.In, h: Hub) -> IO(Hub): + do IO: + h2 : Hub <- Far.tell(Far.in.put(h, i), FR.In.msg(i)) + Far.pull(h2, FR.In.link(i), FR.In.head(i)) + +def Far.loaded.choose(r: FR.In & Hub, disk: FR.In) -> IO(Hub): + (live, h) = r + Far.loaded.put(FR.In.loaded(live, disk), h) + +def Hub.far.loaded(h: Hub, +link: String, +name: String, +text: String) -> IO(Hub): + Far.loaded.choose(Far.in.of(Far.fh(h), link, name), FR.Fr.file.read(text, link, name)) + +def Far.here.was.k(away: Bool, h: Hub, +link: String) -> IO(Hub): + match away: + case True{}: + Far.pull.here(h, link) + case False{}: + IO.pure(Hub, h) + +def Far.here.was(r: FR.In & Hub, +link: String) -> IO(Hub): + (i, h) = r + Far.here.was.k(FR.In.away(i), h, link) + +# The minute: linked machines that answered pull what they lack (a mirror +# that was away, or none yet); those that did not are away +def Far.here.one(ok: Bool, h: Hub, +link: String) -> IO(Hub): + match ok: + case True{}: + Far.here.was(Far.in.of(Far.fh(h), link, link), link) + case False{}: + Far.away(h, link) + +def Far.here.go(ps: List<&2, BO.Peer>, h: Hub, +ok: List<&2, String>) -> IO(Hub): + match ps: + case Nil{}: + IO.pure(Hub, h) + case BO.Peer{+i, _, _, _, _} <> t: + IO.bind(Hub, Hub, Far.here.one(FR.Fr.may.go(ok, i), h, i), h2 => Far.here.go(t, h2, ok)) + +def Far.live(ps: List<&2, BO.Peer>) -> List<&2, BO.Peer>: + match ps: + case Nil{}: + Nil{} + case +p <> t: + Far.keep(Bot.peer.live.k(p), p, Far.live(t)) + +def Hub.far.here(h: Hub, +answered: String) -> IO(Hub): + match h: + case Hub{+st, f, ls, cl, as, inbox, cfg, note}: + Far.here.go(Far.live(BO.World.peers(M.State.bots(st))), Hub{st, f, ls, cl, as, inbox, cfg, note}, String.lines(answered)) + +# At start: this hub's feed is made off the hub; each link's mirror is +# read back from disk (then pulled) +def Far.start.go(ps: List<&2, BO.Peer>, +home: String, +inbox: Chan(Msg)) -> IO(Unit): + match ps: + case Nil{}: + IO.pure(Unit, Unit{}) + case BO.Peer{+i, +n, _, _, _} <> t: + do IO: + IO.spawn(Unit, Far.load.job(i, n, home, inbox)) + Far.start.go(t, home, inbox) + +def Far.start.epoch(p: FR.Fh & Hub, +ls: List<&2, String>) -> Hub: + (fh, h) = p + Far.with(h, FR.Fh.epoch.set(fh, Far.history(ls))) + +def Far.start(h: Hub) -> IO(Hub): + match h: + case Hub{+st, f, +ls, cl, as, +inbox, +cfg, note}: + do IO: + IO.spawn(Unit, Far.feed.job(ls, st, inbox)) + Far.start.go(Far.live(BO.World.peers(M.State.bots(st))), Cfg.home(cfg), inbox) + return Far.start.epoch(Far.fh(Hub{st, f, ls, cl, as, inbox, cfg, note}), ls) + +# A client joined: every mirror, whole +def Far.join.go(ms: List<&2, FR.In>, +out: Chan(Wire)) -> IO(Unit): + match ms: + case Nil{}: + IO.pure(Unit, Unit{}) + case +i <> t: + do IO: + ok : Bool <- Chan.send(Wire, out, WText{FR.In.msg(i)}) + Far.join.go(t, out) + +def Far.join(+cfg: Cfg, +out: Chan(Wire)) -> IO(Unit): + Far.join.go(FR.Fh.ins(BN.Bn.far(Cfg.bn(cfg))), out) + +# the window in this process: the read model (devices left out, as the +# wire leaves them) and the log message with no items +def Hub.join.here(h: Hub, +client: U32, +out: Chan(Wire), +now: Nat) -> IO(Hub): + match h: + case Hub{+st, f, +ls, +cl, as, inbox, +cfg, +note}: + do IO: + ok : Bool <- Chan.send(Wire, out, WState{M.State.private(st), Hub.log_msg.k(0n, ls, cfg, 0n)}) + Far.join(cfg, out) + Hub.join.note(out, note) + Hub.join.note(out, Desk.join(DK.Desk.text(Clients.looks(cl), now))) + return Hub{st, f, ls, Client{client, out, DK.Look.none()} <> cl, as, inbox, cfg, note} + +def Hub.join(h: Hub, +client: U32, +out: Chan(Wire), +since: Nat, +origin: String, +now: Nat) -> IO(Hub): + match h: + case Hub{st, f, +ls, +cl, as, inbox, +cfg, +note}: + do IO: + ok : Bool <- Chan.send(Wire, out, WText{Hub.log_msg(ls, cfg, Bool.pick(Nat, String.eq(origin, Hub.origin(ls)), since, 0n))}) + Far.join(cfg, out) + Hub.join.note(out, note) + Hub.join.note(out, Desk.join(DK.Desk.text(Clients.looks(cl), now))) + return Hub{st, f, ls, Client{client, out, DK.Look.none()} <> cl, as, inbox, cfg, note} + +def Far.rpc.may(ok: Bool, h: Hub, +client: U32, +id: String, +link: String, +body: String, +now: Nat) -> IO(Hub): + match ok h: + case True{} Hub{+st, f, ls, cl, as, +inbox, +cfg, note}: + do IO: + IO.spawn(Unit, Far.rpc.job(client, id, link, body, Cfg.home(cfg), Bot.peer.url(st, link), inbox)) + return Hub{st, f, ls, cl, as, inbox, cfg, note} + case False{} _: + Hub.far.replied(h, client, id, link, 403n, "that is done on its own machine") + +def Hub.announce.far(r: Hub & List<&2, String>, cs: List<&2, M.Change>, +from: Nat) -> IO(Hub): + (h, +fresh) = r + IO.bind(Hub, Hub, Hub.announce((h, fresh)), h2 => Far.push(h2, cs, fresh, from)) + +def Hub.step.go(+st: M.State, h: Hub, cs: List<&2, M.Change>, os: List<&2, B.Out>) -> IO(Hub): do IO: - r : Hub & List<&2, String> <- Hub.commit(B.Pend.fix(st, cs), h, Nil{}) - h2 : Hub <- Hub.announce(r) + +cs2 : List<&2, M.Change> = B.Pend.fix(st, cs) + r : Hub & List<&2, String> <- Hub.commit(cs2, h, Nil{}) + h2 : Hub <- Hub.announce.far(r, cs2, M.State.seq(st)) Hub.outs(os, h2) +def Hub.step.k(p: M.State & Hub, cs: List<&2, M.Change>, os: List<&2, B.Out>) -> IO(Hub): + (st, h) = p + Hub.step.go(st, h, cs, os) + # a step's changes are stored (with the pending counts they move), then # its effects run def Hub.step(h: Hub, s: B.Step) -> IO(Hub): @@ -3998,9 +4595,36 @@ def Hub.rpc.k(p: M.State & Hub, +client: U32, +text: String, +now: Nat) -> IO(Hu (st, h) = p Hub.step(h, Bot.request(J.Json.parse(text), st, now, client, text)) -def Hub.rpc(h: Hub, +client: U32, +text: String, +now: Nat) -> IO(Hub): +def Hub.rpc.here(h: Hub, +client: U32, +text: String, +now: Nat) -> IO(Hub): Hub.rpc.k(Hub.state(h), client, text, now) +# A client's request about something elsewhere goes to its machine; any +# other stays here +def Far.rpc.go(m: Maybe<&2, FR.Route>, h: Hub, +client: U32, +o: J.Json, +text: String, +now: Nat) -> IO(Hub): + match m h: + case Some{FR.Route{+link, +meth, +body}} Hub{+st, f, ls, cl, as, +inbox, +cfg, note}: + Far.rpc.may(FR.Fr.may(meth), Hub{st, f, ls, cl, as, inbox, cfg, note}, client, J.Json.as_raw(J.Json.get(o, "id"), "0"), link, body, now) + case None{} _: + Hub.rpc.here(h, client, text, now) + +def Hub.rpc.o(m: Maybe<&2, J.Json>, h: Hub, +client: U32, +text: String, +now: Nat) -> IO(Hub): + match m: + case Some{+o}: + Far.rpc.go(FR.Fr.route(o), h, client, o, text, now) + case None{}: + Hub.rpc.here(h, client, text, now) + +# a request about something on a linked machine goes there (Far.rpc.go) +def Hub.rpc.far(maybe: Bool, h: Hub, +client: U32, +text: String, +now: Nat) -> IO(Hub): + match maybe: + case True{}: + Hub.rpc.o(J.Json.parse(text), h, client, text, now) + case False{}: + Hub.rpc.here(h, client, text, now) + +def Hub.rpc(h: Hub, +client: U32, +text: String, +now: Nat) -> IO(Hub): + Hub.rpc.far(String.contains(text, "~"), h, client, text, now) + def Hub.line.k(p: M.State & Hub, +thread: String, +line: String, +now: Nat) -> IO(Hub): (st, h) = p Hub.step(h, B.Hub.agent_line(st, now, thread, line)) @@ -4086,38 +4710,124 @@ def Hub.recover.carry(h: Hub, +before: M.State, +now: Nat) -> IO(Hub): def Hub.recover.go(h: Hub, st: B.Step, before: M.State, +now: Nat) -> IO(Hub): IO.bind(Hub, Hub, Hub.step(h, st), h2 => Hub.recover.carry(h2, before, now)) -def Hub.recover(h: Hub, +now: Nat) -> IO(Hub): +def Hub.recover.k(h: Hub, +now: Nat) -> IO(Hub): match h: case Hub{+st, f, ls, cl, as, inbox, cfg, note}: Hub.recover.go(Hub{st, f, ls, cl, as, inbox, cfg, note}, B.Hub.recover(st, now), st, now) +# (and threads elsewhere start: Far.start) +def Hub.recover(h: Hub, +now: Nat) -> IO(Hub): + IO.bind(Hub, Hub, Far.start(h), h2 => Hub.recover.k(h2, now)) + # The minute # ---------- -def Bot.dir.job(ps: List<&2, BO.Peer>, +home: String, +now: Nat, +prev: String, +inbox: Chan(Msg)) -> IO(Unit): - IO.bind(String, Unit, BN.Bn.dir.fetch(ps, home, now, prev), d => IO.bind(Bool, Unit, Chan.send(Msg, inbox, BotsFound{d}), ok => IO.pure(Unit, Unit{}))) +# Each worker publishes as soon as its peer answers. The hub owns slots; +# no worker reads or replaces a snapshot of another worker's result. +def Bot.dir.job(+peer: String, +url: String, +pname: String, +home: String, +inbox: Chan(Msg)) -> IO(Unit): + do IO: + +now : Nat <- Now.nat() + r : BN.Resp <- BN.Bn.dir.fetch(home, url, peer, now) + seen : Nat <- Now.nat() + ok : Bool <- Chan.send(Msg, inbox, BotsFound{peer, url, BN.Bn.fetched(r, peer, pname, seen)}) + return Unit{} -# routines that are due run, and the linked machines are asked for their bots -def Hub.bot_tick(h: Hub, +tz: String, +now: Nat) -> IO(Hub): +def Bot.dir.launch(ids: List<&2, String>, +st: M.State, +cfg: Cfg, +inbox: Chan(Msg)) -> IO(Unit): + match ids: + case Nil{}: + IO.pure(Unit, Unit{}) + case +i <> t: + do IO: + IO.spawn(Unit, Bot.dir.job(i, Bot.peer.url(st, i), Bot.peer.name(st, i), Cfg.home(cfg), inbox)) + Bot.dir.launch(t, st, cfg, inbox) + +def Hub.dir.plan(h: Hub, +plan: BH.DirPlan) -> IO(Hub): match h: case Hub{+st, f, ls, cl, as, +inbox, +cfg, note}: do IO: - IO.spawn(Unit, Bot.dir.job(BO.World.peers(M.State.bots(st)), Cfg.home(cfg), now, BN.Bn.dir(Cfg.bn(cfg)), inbox)) - Hub.step(Hub{st, f, ls, cl, as, inbox, cfg, note}, BH.Bots.tick(st, now, BH.Tz{M.Snooze.tz.secs(tz), M.Snooze.tz.east(tz)})) + Bot.dir.launch(BH.DirPlan.launch(plan), st, cfg, inbox) + return Hub{st, f, ls, cl, as, inbox, Cfg.with_bn(cfg, BN.Bn.with_work(Cfg.bn(cfg), BH.DirPlan.work(plan))), note} -def Hub.bots_found.k(same: Bool, h: Hub, d: String) -> IO(Hub): +def Hub.dir.queue(h: Hub, ps: List<&2, BO.Peer>) -> IO(Hub): + match h: + case Hub{st, f, ls, cl, as, inbox, +cfg, note}: + Hub.dir.plan(Hub{st, f, ls, cl, as, inbox, cfg, note}, BH.DirWork.plan(BH.DirWork.queue(ps, BN.Bn.work(Cfg.bn(cfg))))) + +def Hub.bots_found.k(same: Bool, h: Hub, +d: String) -> IO(Hub): match same h: case True{} _: IO.pure(Hub, h) case False{} Hub{st, f, ls, cl, as, inbox, +cfg, note}: - Hub.models.k(Hub{st, f, ls, cl, as, inbox, Cfg.with_bn(cfg, BN.Bn.with_dir(Cfg.bn(cfg), d)), note}) + do IO: + BN.Bn.cache.save(Cfg.home(cfg), d) + Hub.models.k(Hub{st, f, ls, cl, as, inbox, Cfg.with_bn(cfg, BN.Bn.with_dir(Cfg.bn(cfg), d)), note}) -# the linked machines' bots changed: every client hears (info "bots.remote") -def Hub.bots_found(h: Hub, +d: String) -> IO(Hub): +# Compare with the current directory, never the worker's starting copy. +def Hub.dir.publish(h: Hub, +d: String) -> IO(Hub): match h: case Hub{st, f, ls, cl, as, inbox, +cfg, note}: Hub.bots_found.k(String.eq(BN.Bn.dir(Cfg.bn(cfg)), d), Hub{st, f, ls, cl, as, inbox, cfg, note}, d) +# Only the result of a still-linked peer at the same URL is accepted. +# Always release its slot, including for failures and revoked links. +def Hub.bots_found(h: Hub, +peer: String, +url: String, +result: Maybe<&2, List<&2, J.Json>>) -> IO(Hub): + match h: + case Hub{+st, f, ls, cl, as, inbox, +cfg, note}: + do IO: + +prev : List<&2, J.Json> = J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse(BN.Bn.dir(Cfg.bn(cfg))), J.Null{})) + +rows : List<&2, J.Json> = BH.Dir.accept(Bool.and(Bot.peer.live(st, peer), String.eq(Bot.peer.url(st, peer), url)), result, prev, peer) + h2 : Hub <- Hub.dir.publish(Hub{st, f, ls, cl, as, inbox, + Cfg.with_bn(cfg, BN.Bn.with_work(Cfg.bn(cfg), BH.DirWork.finish(BN.Bn.work(Cfg.bn(cfg)), peer))), note}, + J.Json.show(J.Json.arr(BH.Dir.linked(rows, BO.World.peers(M.State.bots(st)))))) + h3 : Hub <- Far.here.one(Bool.and(Bool.and(Bot.peer.live(st, peer), String.eq(Bot.peer.url(st, peer), url)), Maybe.is_some(&2, List<&2, J.Json>, result)), h2, peer) + Hub.dir.queue(h3, Nil{}) + +# Link changes prune the cache too, so revoked peers stay gone on restart. +def Hub.dir.refresh(h: Hub, +peer: String) -> IO(Hub): + match h: + case Hub{+st, f, ls, cl, as, inbox, +cfg, note}: + do IO: + +ps : List<&2, BO.Peer> = BO.World.peers(M.State.bots(st)) + h2 : Hub <- Hub.dir.publish(Hub{st, f, ls, cl, as, inbox, cfg, note}, J.Json.show(J.Json.arr(BH.Dir.linked( + J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse(BN.Bn.dir(Cfg.bn(cfg))), J.Null{})), ps)))) + Hub.dir.queue(h2, BH.Dir.request(ps, peer)) + +# Routines keep their minute cadence; the ticker's first message also +# refreshes directories immediately after the hub/window has opened. +def Hub.bot_tick(h: Hub, +tz: String, +now: Nat) -> IO(Hub): + match h: + case Hub{+st, f, ls, cl, as, inbox, cfg, note}: + do IO: + h2 : Hub <- Hub.dir.refresh(Hub{st, f, ls, cl, as, inbox, cfg, note}, "") + Hub.step(h2, BH.Bots.tick(st, now, BH.Tz{M.Snooze.tz.secs(tz), M.Snooze.tz.east(tz)})) + +# Discovery and signed inbound traffic are return signals. Fetch just the +# returning peer, and never bounce every successful directory request. +def Bot.dir.machines(n: Net) -> String: + match n: + case Net{_, _, _, _, _, _, _, _, _, mc, _, _, _, _, _}: + mc + +def Hub.dir.returned(h: Hub, +mc: String) -> IO(Hub): + match h: + case Hub{+st, f, ls, cl, as, inbox, +cfg, note}: + do IO: + h2 : Hub <- Hub.machines(Hub{st, f, ls, cl, as, inbox, cfg, note}, mc) + +returned : List<&2, BO.Peer> = BH.Dir.returned(BO.World.peers(M.State.bots(st)), + J.Json.elems(J.Json.get_obj(Maybe.default(&2, J.Json, J.Json.parse(Bot.dir.machines(Cfg.net(cfg))), J.Null{}), "peers")), + J.Json.elems(J.Json.get_obj(Maybe.default(&2, J.Json, J.Json.parse(mc), J.Null{}), "peers"))) + h3 : Hub <- Hub.dir.queue(h2, returned) + Far.returned(h3, returned) + +def Bot.dir.awake(refresh: Bool, +inbox: Chan(Msg), +peer: String) -> IO(Unit): + match refresh: + case True{}: + do IO: + ok : Bool <- Chan.send(Msg, inbox, BotsRefresh{peer}) + return Unit{} + case False{}: + IO.pure(Unit, Unit{}) + # Requests from outside # --------------------- # A webhook (POST /hook/) or a linked machine (POST /bots/deliver, @@ -4151,6 +4861,93 @@ def Hub.answer(h: Hub, +reply: Chan(List<&2, U32>), bs: List<&2, U32>) -> IO(Hub def Bot.json(+status: String, +text: String) -> List<&2, U32>: W.Http.response(status, "application/json", U.Utf8.encode(text), "") +# The requests between hubs +# ------------------------- + +# POST /far/push: a batch from a linked machine (the owner's own) +# A push from another history is only a resync hint. Ask the owner for +# its current epoch instead of trusting a potentially late old push. +def Far.push.receive.k(changed: Bool, h: Hub, +link: String, +since: Nat, +head: Nat, +xs: List<&2, FR.Item>, +name: String) -> IO(Hub): + match changed: + case True{}: + Far.pull.here(h, link) + case False{}: + Far.take(h, link, since, head, xs, name) + +def Far.push.receive(r: FR.In & Hub, +epoch: String, +since: Nat, +head: Nat, +xs: List<&2, FR.Item>, +name: String) -> IO(Hub): + (+i, h) = r + Far.push.receive.k(FR.Fr.epoch.changed(FR.In.epoch(i), epoch), h, FR.In.link(i), since, head, xs, name) + +def Hub.far.push.in(ok: Bool, m: Maybe<&2, J.Json>, h: Hub, +peer: String, +reply: Chan(List<&2, U32>)) -> IO(Hub): + match ok m: + case True{} Some{+o}: + do IO: + Bot.answer(reply, Bot.json("200 OK", "{\"ok\":true}")) + Far.push.receive(Far.in.of(Far.fh(h), peer, BN.Bn.clean(J.Json.get_str(o, "name", ""))), J.Json.get_str(o, "epoch", ""), J.Json.get_nat(o, "since", 0n), J.Json.get_nat(o, "head", 0n), FR.Fr.items.of(J.Json.elems(J.Json.get_obj(o, "items"))), + BN.Bn.clean(J.Json.get_str(o, "name", ""))) + case True{} None{}: + Hub.answer(h, reply, W.Http.text("400 Bad Request", "not JSON")) + case False{} _: + Hub.answer(h, reply, W.Http.text("403 Forbidden", "threads are shared with the owner's own machines")) + +def Far.log.go(m: Maybe<&2, List<&2, FR.Line>>, h: Hub, +since: Nat, +epoch: String, +reply: Chan(List<&2, U32>)) -> IO(Hub): + match m h: + case Some{+fs} Hub{+st, f, ls, cl, as, inbox, +cfg, note}: + +own = FR.Fh.epoch(BN.Bn.far(Cfg.bn(cfg))) + +from = Bool.pick(Nat, FR.Fr.epoch.changed(epoch, own), 0n, since) + Hub.answer(Hub{st, f, ls, cl, as, inbox, cfg, note}, reply, Bot.json("200 OK", J.Json.show(J.Obj{J.Field{"since", J.Json.nat(from), + FR.Fr.epoch.fields(Maybe.default(&2, J.Json, J.Json.parse(FR.Fr.with.epoch(FR.Page.json(FR.Fr.page(fs, from, M.State.seq(st), 400000n)), own)), J.Null{}))}}))) + case None{} _: + Hub.answer(h, reply, W.Http.text("503 Service Unavailable", "not ready")) + +def Far.log.k(r: Maybe<&2, List<&2, FR.Line>> & Hub, +since: Nat, +epoch: String, +reply: Chan(List<&2, U32>)) -> IO(Hub): + (m, h) = r + Far.log.go(m, h, since, epoch, reply) + +# POST /far/log: a page of what this hub shares after `since` +def Hub.far.log(ok: Bool, m: Maybe<&2, J.Json>, h: Hub, +reply: Chan(List<&2, U32>)) -> IO(Hub): + match ok m: + case True{} Some{+o}: + Far.log.k(Far.feed.of(Far.fh(h)), J.Json.get_nat(o, "since", 0n), J.Json.get_str(o, "epoch", ""), reply) + case True{} None{}: + Hub.answer(h, reply, W.Http.text("400 Bad Request", "not JSON")) + case False{} _: + Hub.answer(h, reply, W.Http.text("403 Forbidden", "threads are shared with the owner's own machines")) + +# Extract the actual decider reply before sending the step's effects. +def Far.rpc.reply(os: List<&2, B.Out>) -> String: + match os: + case Nil{}: + "{\"t\":\"reply\",\"id\":0,\"ok\":false,\"error\":\"no reply from owning hub\"}" + case B.Send{_, text} <> _: + text + case _ <> t: + Far.rpc.reply(t) + +def Hub.far.rpc.step(step: B.Step, h: Hub, +reply: Chan(List<&2, U32>)) -> IO(Hub): + match step: + case B.Step{cs, +os}: + do IO: + h2 : Hub <- Hub.step(h, B.Step{cs, os}) + Hub.answer(h2, reply, Bot.json("200 OK", Far.rpc.reply(os))) + +def Hub.far.rpc.k(may: Bool, h: Hub, +o: J.Json, +text: String, +reply: Chan(List<&2, U32>), +now: Nat) -> IO(Hub): + match may h: + case True{} Hub{+st, f, ls, cl, as, inbox, cfg, note}: + Hub.far.rpc.step(Bot.request(Some{o}, st, now, Far.client(), text), Hub{st, f, ls, cl, as, inbox, cfg, note}, reply) + case False{} _: + Hub.answer(h, reply, W.Http.text("403 Forbidden", "not something this machine shares")) + +# POST /far/rpc: a request about one of this hub's shared threads (Fr.allowed) +def Hub.far.rpc(ok: Bool, m: Maybe<&2, J.Json>, h: Hub, +text: String, +reply: Chan(List<&2, U32>), +now: Nat) -> IO(Hub): + match ok m h: + case True{} Some{+o} Hub{+st, f, ls, cl, as, inbox, cfg, note}: + Hub.far.rpc.k(FR.Fr.allowed(st, o), Hub{st, f, ls, cl, as, inbox, cfg, note}, o, text, reply, now) + case True{} None{} _: + Hub.answer(h, reply, W.Http.text("400 Bad Request", "not JSON")) + case False{} _ _: + Hub.answer(h, reply, W.Http.text("403 Forbidden", "threads are shared with the owner's own machines")) + def Bot.shot.sent(bs: List<&2, U32>, +reply: Chan(List<&2, U32>)) -> IO(Unit): match bs: case Nil{}: @@ -4309,11 +5106,17 @@ def Bot.page.sent(none: Bool, +html: String, +reply: Chan(List<&2, U32>)) -> IO( "Content-Security-Policy: " ++ SPC.Space.page.csp() ++ "\r\nX-Content-Type-Options: nosniff\r\n")) # a bot's space page (Space.page: no scripts, nothing from the network) +# a bot's page: one here from the read model, one mirrored from a linked +# machine ("~") from its mirror +def Hub.bot.page.html(+st: M.State, +cfg: Cfg, +bot: String) -> String: + Bool.pick(String, FR.Fr.is(bot), FR.In.page(FR.Fh.in(BN.Bn.far(Cfg.bn(cfg)), FR.Fr.link(bot), ""), FR.Fr.local(bot)), + BO.Page.html(BO.World.spage(M.State.bots(st), bot))) + def Hub.bot.page(h: Hub, +bot: String, +reply: Chan(List<&2, U32>)) -> IO(Hub): match h: - case Hub{+st, f, ls, cl, as, inbox, cfg, note}: + case Hub{+st, f, ls, cl, as, inbox, +cfg, note}: do IO: - +html : String = BO.Page.html(BO.World.spage(M.State.bots(st), bot)) + +html : String = Hub.bot.page.html(st, cfg, bot) IO.spawn(Unit, Bot.page.sent(Bool.or(String.is_empty(bot), String.is_empty(html)), html, reply)) return Hub{st, f, ls, cl, as, inbox, cfg, note} @@ -4401,7 +5204,7 @@ def Hub.bot.dir(live: Bool, h: Hub, +reply: Chan(List<&2, U32>)) -> IO(Hub): case False{} _: Hub.answer(h, reply, W.Http.text("401 Unauthorized", "not a linked machine")) -def Hub.bot.act(i: Nat, h: Hub, +arg: String, body: String, +me: String, +reply: Chan(List<&2, U32>), +now: Nat) -> IO(Hub): +def Hub.bot.act(i: Nat, h: Hub, +arg: String, +body: String, +me: String, +reply: Chan(List<&2, U32>), +now: Nat) -> IO(Hub): match i h: case 0n Hub{+st, f, ls, cl, as, inbox, cfg, note}: Hub.bot.hook(Bot.hook.live(st, arg), Hub{st, f, ls, cl, as, inbox, cfg, note}, arg, body, String.eq(me, "ring"), reply, now) @@ -4411,6 +5214,12 @@ def Hub.bot.act(i: Nat, h: Hub, +arg: String, body: String, +me: String, +reply: Hub.bot.link(Bool.not(Bot.peer.live(st, arg)), J.Json.parse(body), Hub{st, f, ls, cl, as, inbox, cfg, note}, arg, me, reply, now) case 3n Hub{+st, f, ls, cl, as, inbox, cfg, note}: Hub.bot.dir(Bot.peer.live(st, arg), Hub{st, f, ls, cl, as, inbox, cfg, note}, reply) + case 4n Hub{+st, f, ls, cl, as, inbox, +cfg, note}: + Hub.far.push.in(Far.own.id(st, cfg, arg), J.Json.parse(body), Hub{st, f, ls, cl, as, inbox, cfg, note}, arg, reply) + case 5n Hub{+st, f, ls, cl, as, inbox, +cfg, note}: + Hub.far.log(Far.own.id(st, cfg, arg), J.Json.parse(body), Hub{st, f, ls, cl, as, inbox, cfg, note}, reply) + case 6n Hub{+st, f, ls, cl, as, inbox, +cfg, note}: + Hub.far.rpc(Far.own.id(st, cfg, arg), J.Json.parse(body), Hub{st, f, ls, cl, as, inbox, cfg, note}, body, reply, now) case _ _: Hub.answer(h, reply, W.Http.text("404 Not Found", "not found")) @@ -4420,8 +5229,11 @@ def Hub.bot.seen(bad: Bool, m: Maybe<&2, SG.Seen>, h: Hub, +why: String, +kind: Hub.answer(h, reply, W.Http.text("401 Unauthorized", why)) case False{} None{} _: Hub.answer(h, reply, W.Http.text("401 Unauthorized", "already seen")) - case False{} Some{s} Hub{st, f, ls, cl, as, inbox, +cfg, note}: - Hub.bot.act(C.Codec.pos(["hook", "deliver", "link", "dir"], kind), Hub{st, f, ls, cl, as, inbox, Cfg.with_bn(cfg, BN.Bn.with_seen(Cfg.bn(cfg), s)), note}, + case False{} Some{s} Hub{st, f, ls, cl, as, +inbox, +cfg, note}: + do IO: + Bot.dir.awake(Bool.and(Bool.or(String.eq(kind, "dir"), String.eq(kind, "deliver")), BH.Dir.stale( + J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse(BN.Bn.dir(Cfg.bn(cfg))), J.Null{})), arg)), inbox, arg) + Hub.bot.act(C.Codec.pos(["hook", "deliver", "link", "dir", "far-push", "far-log", "far-rpc"], kind), Hub{st, f, ls, cl, as, inbox, Cfg.with_bn(cfg, BN.Bn.with_seen(Cfg.bn(cfg), s)), note}, arg, body, me, reply, now) # a signed request (or a webhook's bearer token), checked: genuine (key), @@ -4444,7 +5256,10 @@ def Conn.bot.kind.k(+post: Bool, +get: Bool, +r: String) -> Nat: Bool.pick(Nat, Bool.and(get, String.eq(r, "/bots/dir")), 4n, Bool.pick(Nat, Bool.and(get, Bool.and(String.starts_with(r, "/bots/"), String.ends_with(r, "/screen.jpg"))), 5n, Bool.pick(Nat, Bool.and(get, String.eq(r, "/oauth/google")), 6n, - Bool.pick(Nat, Bool.and(get, Bool.and(String.starts_with(r, "/bots/"), String.ends_with(r, "/space.html"))), 7n, 0n))))))) + Bool.pick(Nat, Bool.and(get, Bool.and(String.starts_with(r, "/bots/"), String.ends_with(r, "/space.html"))), 7n, + Bool.pick(Nat, Bool.and(post, String.eq(r, "/far/push")), 8n, + Bool.pick(Nat, Bool.and(post, String.eq(r, "/far/log")), 9n, + Bool.pick(Nat, Bool.and(post, String.eq(r, "/far/rpc")), 10n, 0n)))))))))) # which bots' route a request is (0: none) def Conn.bot.kind(req: W.Request) -> Nat: @@ -4457,8 +5272,12 @@ def Conn.bot.shot_id(+r: String) -> String: Bool.pick(String, BN.Bn.safe(String.take(String.drop(r, 6n), Nat.sub(String.length(r), 17n))), String.take(String.drop(r, 6n), Nat.sub(String.length(r), 17n)), "") # /bots//space.html: the id ("" when it is not a plain id) +# (a plain id, or a mirrored bot's "~" of two plain ids) +def Conn.bot.page_ok(+id: String) -> Bool: + Bool.or(BN.Bn.safe(id), Bool.and(FR.Fr.is(id), Bool.and(BN.Bn.safe(FR.Fr.link(id)), BN.Bn.safe(FR.Fr.local(id))))) + def Conn.bot.page_id(+r: String) -> String: - Bool.pick(String, BN.Bn.safe(String.take(String.drop(r, 6n), Nat.sub(String.length(r), 17n))), String.take(String.drop(r, 6n), Nat.sub(String.length(r), 17n)), "") + Bool.pick(String, Conn.bot.page_ok(String.take(String.drop(r, 6n), Nat.sub(String.length(r), 17n))), String.take(String.drop(r, 6n), Nat.sub(String.length(r), 17n)), "") def Conn.bot.answer(m: Maybe<&1, List<&2, U32>>, s: Socket) -> IO(Unit): match m: @@ -4659,6 +5478,12 @@ def Conn.route.bot(k: Nat, s: Socket, +req: W.Request, rest: List<&2, U32>, +web Conn.bot.own(Conn.allowed(trusted, hs, p, token), s, BotReq{"oauth", p, "", "", "", "", ""}, inbox) case 7n W.Request{_, +p, +hs}: Conn.bot.own(Conn.allowed(trusted, hs, p, token), s, BotReq{"page", Conn.bot.page_id(W.Http.route(p)), "", "", "", "", ""}, inbox) + case 8n W.Request{_, _, +hs}: + Conn.bot.signed("far-push", W.Http.header_of(hs, "x-backplane-peer"), s, hs, rest, inbox) + case 9n W.Request{_, _, +hs}: + Conn.bot.signed("far-log", W.Http.header_of(hs, "x-backplane-peer"), s, hs, rest, inbox) + case 10n W.Request{_, _, +hs}: + Conn.bot.signed("far-rpc", W.Http.header_of(hs, "x-backplane-peer"), s, hs, rest, inbox) case _ _: Conn.route.att(Conn.is_att(req), s, req, rest, web, id, inbox, trusted, token) @@ -4689,7 +5514,7 @@ def Conn.head(fuel: Nat, s: Socket, +buf: List<&2, U32>, +web: String, +id: U32, def Accept.whois(r: Socket & String, +owner: String, +id: U32, +web: String, +inbox: Chan(Msg), +token: String) -> IO(Unit): (s, ip) = r do IO: - w : P.Ran <- P.Proc.run("tailscale", ["whois", "--json", ip], "/") + w : P.Ran <- P.Proc.run("timeout", ["--kill-after=1", "5", "tailscale", "whois", "--json", ip], "/") Conn.head(64n, s, Nil{}, web, id, inbox, TN.Tailnet.trusts(owner, TN.Tailnet.login(Tailnet.out(w))), token) def Accept.trust(ask: Bool, s: Socket, +owner: String, +id: U32, +web: String, +inbox: Chan(Msg), +trusted: Bool, +token: String) -> IO(Unit): @@ -4955,8 +5780,10 @@ def Msg.kind(m: Msg) -> String: "commit" case BotTick{_}: "bot_tick" - case BotsFound{_}: + case BotsFound{_, _, _}: "bots_found" + case BotsRefresh{_}: + "bots_refresh" case BotHttp{_, _, _, _, _, _, _, _, _, _, _}: "bot_http" case BotVerified{_, _, _, _, _, _}: @@ -4967,6 +5794,20 @@ def Msg.kind(m: Msg) -> String: "new_project" case AttDir{_, _}: "att_dir" + case FarFeed{_, _}: + "far_feed" + case FarPushed{_, _, _}: + "far_pushed" + case FarPulled{_, _, _, _}: + "far_pulled" + case FarReplied{_, _, _, _, _}: + "far_replied" + case FarLoaded{_, _, _}: + "far_loaded" + case FarHere{_}: + "far_here" + case FarRetry{_}: + "far_retry" def Hub.handle(h: Hub, m: Msg, +now: Nat) -> IO(Hub): match m: @@ -5007,15 +5848,17 @@ def Hub.handle(h: Hub, m: Msg, +now: Nat) -> IO(Hub): case ModelsFound{ms, mt}: Hub.models(h, ms, mt) case MachinesFound{mc}: - Hub.machines(h, mc) + Hub.dir.returned(h, mc) case LinkFound{lk}: Hub.link(h, lk) case Commit{cs}: Hub.step(h, B.Step{cs, Nil{}}) case BotTick{tz}: Hub.bot_tick(h, tz, now) - case BotsFound{d}: - Hub.bots_found(h, d) + case BotsFound{peer, url, result}: + Hub.bots_found(h, peer, url, result) + case BotsRefresh{peer}: + Hub.dir.refresh(h, peer) case BotHttp{kind, arg, peer, ts, sig, gh, dl, body, ct, raw, reply}: Hub.bot_http(h, kind, arg, peer, ts, sig, gh, dl, body, ct, raw, reply, now) case BotVerified{kind, arg, key, body, me, reply}: @@ -5026,6 +5869,20 @@ def Hub.handle(h: Hub, m: Msg, +now: Nat) -> IO(Hub): Hub.broadcast(h, text) case AttDir{thread, reply}: Hub.att_dir(h, thread, reply) + case FarFeed{ls2, epoch}: + Hub.far.fed(h, ls2, epoch) + case FarPushed{link, ok, to}: + Hub.far.pushed(h, link, ok, to) + case FarPulled{link, since, code, body}: + Hub.far.pulled(h, link, since, code, body) + case FarReplied{client, id, link, code, body}: + Hub.far.replied(h, client, id, link, code, body) + case FarLoaded{link, name, text}: + Hub.far.loaded(h, link, name, text) + case FarHere{ans}: + Hub.far.here(h, ans) + case FarRetry{link}: + Far.pull.here(h, link) # each message's handling is timed as hub. (docs/perf.md) def Hub.got(m: Maybe<&1, Msg>, h: Hub, k: Hub -> IO(Unit)) -> IO(Unit): @@ -5177,6 +6034,7 @@ def Main.boot(cfg: Cfg, need: Bool, front: Chan(Msg) -> IO(Unit)) -> IO(Unit): f : Result<&1, &1, U32 & String, File> <- File.open(Store.path(home), "a") tok : String <- Token.load(home) +mc : String <- Files.text(Models.cache(home)) + +bc : String <- BN.Bf.text(BN.Bn.cache(home)) +off : String <- Env.get("BACKPLANE_NO_UPDATE", "") +ai : String <- Env.get("APPIMAGE", "") - Main.opened(f, st, ls, Cfg.with_managed(Cfg.with_models(Cfg.with_token(Cfg{home, w, h, p, pr, ds, v, nt}, tok), BT.Cache.json(mc), BT.Cache.text(mc)), SV.Chan.managed(off, ai)), need, front) + Main.opened(f, st, ls, Cfg.with_managed(Cfg.with_models(Cfg.with_bn(Cfg.with_token(Cfg{home, w, h, p, pr, ds, v, nt}, tok), BN.Bn.with_dir(BN.Bn.empty(), BN.Bn.cache.load(bc, BO.World.peers(M.State.bots(st))))), BT.Cache.json(mc), BT.Cache.text(mc)), SV.Chan.managed(off, ai)), need, front) diff --git a/src/web/app.bend b/src/web/app.bend index 68677426af..063cefe5e4 100644 --- a/src/web/app.bend +++ b/src/web/app.bend @@ -66,7 +66,7 @@ def visit(+u: U.Ui, +url: String) -> String: # the page opened with "#bot=" (decoded): that bot is shown once the # log has it def wanted(+u: U.Ui, +hash: String) -> U.Ui: - Bool.pick(U.Ui, String.starts_with(hash, "#bot="), U.Ui.scratch_set(u, "@bot.want", String.drop(hash, 5n)), u) + Bool.pick(U.Ui, String.starts_with(hash, "#bot="), U.Want.bot(u, String.drop(hash, 5n)), u) def act(u: U.Ui, action: String, value: String) -> U.Act: U.Solid.sync(U.Ui.act(u, action, value)) diff --git a/src/web/bots.bend b/src/web/bots.bend index 17cf7c16b2..30465f8dd6 100644 --- a/src/web/bots.bend +++ b/src/web/bots.bend @@ -72,19 +72,13 @@ def WB.keep(-A: Data, hit: Bool, x: A, rest: List<&2, A>) -> List<&2, A>: # Sidebar # ------- -def WB.rname(remote: Bool, name: String, +peer: String) -> String: - match remote: - case True{}: - name ++ Bool.pick(String, String.is_empty(peer), "", " · " ++ peer) - case False{}: - name - -# a bot: its cat, its name and its mood; a bot elsewhere shows its machine -def WB.row(r: BI.BRow) -> D.Node: +# a bot: its cat, its name and its mood; a bot elsewhere shows its +# machine (BU.row.title); a click does what BU.act says +def WB.row(+r: BI.BRow) -> D.Node: match r: - case BI.BRow{+id, name, _, +look, +mood, note, sel, +remote, peer, _}: - D.El{"div", "bot:" ++ id, [D.At{"class", Bool.pick(String, sel, "bot-row on", "bot-row")}, D.At{"title", id}, D.At{"data-value", id}, D.On{"click", Bool.pick(String, remote, "remote", "bot")}], - [WC.WCat.img(look, mood, 28), WB.div("bot-text", [D.Dom.span("bot-name", WB.rname(remote, name, peer)), D.Dom.span(WB.mood.cls(mood), note)])]} + case BI.BRow{+id, _, _, +look, +mood, note, sel, _, _, _}: + D.El{"div", "bot:" ++ id, [D.At{"class", Bool.pick(String, sel, "bot-row on", "bot-row")}, D.At{"title", id}, D.At{"data-value", id}, D.On{"click", BI.BU.act(r)}], + [WC.WCat.img(look, mood, 28), WB.div("bot-text", [D.Dom.span("bot-name", BI.BU.row.title(r)), D.Dom.span(WB.mood.cls(mood), note)])]} def WB.rows(rs: List<&2, BI.BRow>) -> List<&2, D.Node>: match rs: @@ -170,20 +164,26 @@ def WB.shown(v: BI.View) -> Bool: # A bot # ----- -def WB.head.t(t: M.Thread, +u: U.Ui, +b: BT.Bot) -> D.Node: - match t: - case M.Thread{_, _, _, _, _, turn, _, _, _, _, _, _, _, _, _, +pr, +mo, _, _, _, _}: +# a bot's header (BU.head, the same for a bot here, one mirrored from a +# linked machine and one only reported by one): its cat, its name with +# where it lives, its mood, then sub (its model) and tools (Stop) +def WB.header(hd: BI.Head, sub: List<&2, D.Node>, tools: List<&2, D.Node>) -> D.Node: + match hd: + case BI.Head{+title, +look, +mood, +note, _, _, _, _}: D.El{"header", "", [D.At{"class", "thread-head bot-head"}], [ D.Dom.button("back", "select", "", "Back", "‹"), - WC.WCat.img(BT.Bot.look(b), BI.Mood.bot(U.Ui.state(u), U.Side.now(u), b), 72), - WB.div("bot-id", [D.Dom.span("bot-title", BT.Bot.name(b)), - D.Dom.span(WB.mood.cls(BI.Mood.bot(U.Ui.state(u), U.Side.now(u), b)), BI.Mood.note(BI.Mood.bot(U.Ui.state(u), U.Side.now(u), b))), - D.Dom.span("bot-model", U.Models.label(pr, mo))]), - D.El{"div", "", [D.At{"class", "tools"}], D.Dom.when(WB.running(turn), D.Dom.button("", "interrupt", "", "Stop the running turn", "Stop"))}]} + WC.WCat.img(look, mood, 72), + WB.div("bot-id", D.Dom.span("bot-title", title) <> D.Dom.span(WB.mood.cls(mood), note) <> sub), + D.El{"div", "", [D.At{"class", "tools"}], tools}]} + +def WB.head.t(t: M.Thread, hd: BI.Head) -> D.Node: + match t: + case M.Thread{_, _, _, _, _, turn, _, _, _, _, _, _, _, _, _, +pr, +mo, _, _, _, _}: + WB.header(hd, [D.Dom.span("bot-model", U.Models.label(pr, mo))], D.Dom.when(WB.running(turn), D.Dom.button("", "interrupt", "", "Stop the running turn", "Stop"))) # the bot's header: its cat, name, mood and model -def WB.head(+u: U.Ui, +b: BT.Bot) -> D.Node: - WB.head.t(U.Ui.thread(U.Ui.state(u), BT.Bot.thread(b)), u, b) +def WB.head(+u: U.Ui, +b: BT.Bot, hd: BI.Head) -> D.Node: + WB.head.t(U.Ui.thread(U.Ui.state(u), BT.Bot.thread(b)), hd) def WB.tabs.go(ts: List<&2, String>, +cur: String) -> List<&2, D.Node>: match ts: @@ -192,8 +192,9 @@ def WB.tabs.go(ts: List<&2, String>, +cur: String) -> List<&2, D.Node>: case +t <> r: D.Dom.button(Bool.pick(String, String.eq(t, cur), "tab on", "tab"), "bot-tab", t, BI.BU.tab.label(t), BI.BU.tab.label(t)) <> WB.tabs.go(r, cur) -def WB.tabs(+cur: String) -> D.Node: - D.El{"nav", "", [D.At{"class", "bot-tabs"}], WB.tabs.go(BI.BU.tabs(), cur)} +# the tabs a bot has (BU.head), the one shown on +def WB.tabs(+hd: BI.Head) -> D.Node: + D.El{"nav", "", [D.At{"class", "bot-tabs"}], WB.tabs.go(BI.Head.tabs(hd), BI.Head.tab(hd))} # Browser: the bot's page, a frame the hub serves (the pairing cookie # carries the token, as for /img); n changes with each "shot" message @@ -219,63 +220,71 @@ def WB.kind.of(ms: List<&2, BT.Mem>, +k: String) -> List<&2, BT.Mem>: def WB.tags(+tags: String) -> List<&2, D.Node>: Bool.pick(List<&2, D.Node>, String.is_empty(tags), Nil{}, [D.Dom.span("item-tags", tags)]) -def WB.mem(m: BT.Mem, +now: Nat) -> D.Node: +# a memory; Forget only for a bot here (BU.owns) +def WB.mem(m: BT.Mem, +own: Bool, +now: Nat) -> D.Node: match m: case BT.Mem{_, +key, _, text, +tags, _, +up}: D.El{"div", "mem:" ++ key, [D.At{"class", "item"}], [ WB.div("item-main", D.Dom.cat([D.Dom.span("item-key", key), D.Dom.span("item-text", text)], WB.tags(tags))), D.Dom.span("ago", WB.ago(now, up)), - WB.div("item-btns", [D.Dom.button("", "mem-forget", key, "Forget this", "Forget")])]} + WB.div("item-btns", D.Dom.when(own, D.Dom.button("", "mem-forget", key, "Forget this", "Forget")))]} -def WB.mems(ms: List<&2, BT.Mem>, +now: Nat) -> List<&2, D.Node>: +def WB.mems(ms: List<&2, BT.Mem>, +own: Bool, +now: Nat) -> List<&2, D.Node>: match ms: case Nil{}: Nil{} case m <> t: - WB.mem(m, now) <> WB.mems(t, now) + WB.mem(m, own, now) <> WB.mems(t, own, now) -def WB.mgroup(ms: List<&2, BT.Mem>, +k: String, +now: Nat) -> List<&2, D.Node>: +def WB.mgroup(ms: List<&2, BT.Mem>, +k: String, +own: Bool, +now: Nat) -> List<&2, D.Node>: match ms: case Nil{}: Nil{} case m <> t: - [WB.div("group", D.Dom.span("group-label", k) <> WB.mems(m <> t, now))] + [WB.div("group", D.Dom.span("group-label", k) <> WB.mems(m <> t, own, now))] -def WB.mgroups(ks: List<&2, String>, +ms: List<&2, BT.Mem>, +now: Nat) -> List<&2, D.Node>: +def WB.mgroups(ks: List<&2, String>, +ms: List<&2, BT.Mem>, +own: Bool, +now: Nat) -> List<&2, D.Node>: match ks: case Nil{}: Nil{} case +k <> t: - List.append(&2, D.Node, WB.mgroup(WB.kind.of(ms, k), k, now), WB.mgroups(t, ms, now)) + List.append(&2, D.Node, WB.mgroup(WB.kind.of(ms, k), k, own, now), WB.mgroups(t, ms, own, now)) -def WB.memory.k(+ms: List<&2, BT.Mem>, +now: Nat) -> D.Node: - WB.div("bot-list", Bool.pick(List<&2, D.Node>, List.is_empty(&2, BT.Mem, ms), [WB.note("Nothing remembered yet.")], WB.mgroups(BT.Mem.kinds(), ms, now))) +def WB.memory.k(+ms: List<&2, BT.Mem>, +own: Bool, +now: Nat) -> D.Node: + WB.div("bot-list", Bool.pick(List<&2, D.Node>, List.is_empty(&2, BT.Mem, ms), [WB.note("Nothing remembered yet.")], WB.mgroups(BT.Mem.kinds(), ms, own, now))) def WB.memory(+u: U.Ui, +b: BT.Bot) -> D.Node: - WB.memory.k(BI.BU.mems(u, BT.Bot.id(b)), U.Side.now(u)) + WB.memory.k(BI.BU.mems(u, BT.Bot.id(b)), BI.BU.owns(BT.Bot.id(b)), U.Side.now(u)) # Routines def WB.when(+cron: String) -> String: Bool.pick(String, String.is_empty(CR.Cron.describe(cron)), cron, CR.Cron.describe(cron)) -def WB.routine(r: BT.Routine, +now: Nat) -> D.Node: +# a routine's buttons: Run now for any bot, the rest for a bot here +def WB.routine.btns(own: Bool, +id: String, +on: Bool) -> List<&2, D.Node>: + match own: + case True{}: + [D.Dom.button(Bool.pick(String, on, "on", ""), "routine-toggle", id, Bool.pick(String, on, "Turn off", "Turn on"), Bool.pick(String, on, "On", "Off")), + D.Dom.button("", "routine-run", id, "Run it now", "Run now"), + D.Dom.button("", "routine-edit", id, "Edit", "Edit"), + D.Dom.button("", "routine-delete", id, "Delete", "Delete")] + case False{}: + [D.Dom.button("", "routine-run", id, "Run it now", "Run now")] + +def WB.routine(r: BT.Routine, +own: Bool, +now: Nat) -> D.Node: match r: case BT.Routine{+id, _, name, +cron, prompt, +on, +last, _, _}: D.El{"div", "ro:" ++ id, [D.At{"class", Bool.pick(String, on, "item", "item off")}], [ WB.div("item-main", [D.Dom.span("item-key", name), D.Dom.span("item-when", WB.when(cron)), D.Dom.span("item-text", prompt)]), D.El{"span", "", [D.At{"class", "ago"}, D.At{"title", "Last run"}], [D.Txt{WB.ago(now, last)}]}, - WB.div("item-btns", [ - D.Dom.button(Bool.pick(String, on, "on", ""), "routine-toggle", id, Bool.pick(String, on, "Turn off", "Turn on"), Bool.pick(String, on, "On", "Off")), - D.Dom.button("", "routine-run", id, "Run it now", "Run now"), - D.Dom.button("", "routine-edit", id, "Edit", "Edit"), - D.Dom.button("", "routine-delete", id, "Delete", "Delete")])]} + WB.div("item-btns", WB.routine.btns(own, id, on))]} -def WB.routines.go(rs: List<&2, BT.Routine>, +now: Nat) -> List<&2, D.Node>: +def WB.routines.go(rs: List<&2, BT.Routine>, +own: Bool, +now: Nat) -> List<&2, D.Node>: match rs: case Nil{}: Nil{} case r <> t: - WB.routine(r, now) <> WB.routines.go(t, now) + WB.routine(r, own, now) <> WB.routines.go(t, own, now) def WB.cron.hint.k(empty: Bool, bad: Bool, +d: String) -> List<&2, D.Node>: match empty bad: @@ -300,7 +309,7 @@ def WB.rform(+u: U.Ui) -> D.Node: D.Dom.button("", "routine-edit", "", "Clear the form", "Clear")])]))) def WB.routines(+u: U.Ui, +b: BT.Bot) -> D.Node: - WB.div("bot-list", List.append(&2, D.Node, WB.routines.go(BI.BU.routines(u, BT.Bot.id(b)), U.Side.now(u)), [WB.rform(u)])) + WB.div("bot-list", List.append(&2, D.Node, WB.routines.go(BI.BU.routines(u, BT.Bot.id(b)), BI.BU.owns(BT.Bot.id(b)), U.Side.now(u)), D.Dom.when(BI.BU.owns(BT.Bot.id(b)), WB.rform(u)))) # Webhooks def WB.page(+u: U.Ui) -> String: @@ -572,37 +581,15 @@ def WB.room(+u: U.Ui, r: BT.Room) -> D.Node: WB.timeline(Bool.pick(List<&2, D.Node>, List.is_empty(&2, BT.Post, BI.BU.posts(u, id)), [WB.note("No posts yet.")], WB.posts(BI.BU.posts(u, id), u, 0n, "away"))), WB.compose("room-post", "rpost", BI.BU.field(u, "rpost"), "Write to the room", "room-post")]} -def WB.far.find(rs: List<&2, BI.BRow>, +id: String) -> Maybe<&2, BI.BRow>: - match rs: - case Nil{}: - None{} - case BI.BRow{+i, n, th, lk, mo, no, s, re, pe, l} <> t: - BT.Pick.first(BI.BRow, String.eq(i, id), BI.BRow{i, n, th, lk, mo, no, s, re, pe, l}, WB.far.find(t, id)) - -def WB.far.look(m: Maybe<&2, BI.BRow>) -> Nat: - match m: - case Some{BI.BRow{_, _, _, lk, _, _, _, _, _, _}}: - lk - case None{}: - 0n - -def WB.far.mood(m: Maybe<&2, BI.BRow>) -> String: - match m: - case Some{BI.BRow{_, _, _, _, mo, _, _, _, _, _}}: - mo - case None{}: - "away" - -def WB.far.head(m: Maybe<&2, BI.BRow>, +id: String) -> List<&2, D.Node>: - match m: - case Some{BI.BRow{_, _, _, +look, +mood, note, _, _, _, _}}: - [WC.WCat.img(look, mood, 48), WB.div("bot-id", [D.Dom.span("bot-title", id), D.Dom.span(WB.mood.cls(mood), note)])] - case None{}: - [WC.WCat.img(0n, "away", 48), WB.div("bot-id", [D.Dom.span("bot-title", id), D.Dom.span("mood away", "away")])] +# a bot only reported by a linked machine: the bot's view (BU.head: its +# cat, "name · machine", its mood, Chat alone) over the person's +# conversation with it, and a line to it +def WB.remote.h(+u: U.Ui, +id: String, +hd: BI.Head) -> D.Node: + D.El{"main", "remote", [D.At{"class", "thread-view bot-view room-view"}], [ + WB.header(hd, Nil{}, Nil{}), + WB.tabs(hd), + WB.timeline(Bool.pick(List<&2, D.Node>, List.is_empty(&2, BT.Post, BI.BU.remote.posts(u, id)), [WB.note("Nothing said yet.")], WB.posts(BI.BU.remote.posts(u, id), u, BI.Head.look(hd), BI.Head.mood(hd)))), + WB.compose("remote-send", "rtext", BI.BU.field(u, "rtext"), "Write to " ++ id, "remote-send")]} -# a bot on another machine: what you and it said, and a line to it def WB.remote(+u: U.Ui, +id: String) -> D.Node: - D.El{"main", "remote", [D.At{"class", "thread-view room-view"}], [ - D.El{"header", "", [D.At{"class", "thread-head bot-head small"}], D.Dom.button("back", "select", "", "Back", "‹") <> WB.far.head(WB.far.find(BI.BU.remote(u), id), id)}, - WB.timeline(Bool.pick(List<&2, D.Node>, List.is_empty(&2, BT.Post, BI.BU.remote.posts(u, id)), [WB.note("Nothing said yet.")], WB.posts(BI.BU.remote.posts(u, id), u, WB.far.look(WB.far.find(BI.BU.remote(u), id)), WB.far.mood(WB.far.find(BI.BU.remote(u), id))))), - WB.compose("remote-send", "rtext", BI.BU.field(u, "rtext"), "Write to " ++ id, "remote-send")]} + WB.remote.h(u, id, BI.BU.head(u, BI.VRemote{id})) diff --git a/src/web/view.bend b/src/web/view.bend index a4e8a4d27e..5ca8c81e52 100644 --- a/src/web/view.bend +++ b/src/web/view.bend @@ -75,7 +75,7 @@ def View.row.k(+id: String, +title: String, +ago: String, +badge: String, +dot: def View.row(+t: M.Thread, +st: M.State, +sel: String, +now: Nat, +movable: Bool, +snoozed: Bool) -> D.Node: match t: case M.Thread{+id, _, +title, _, +a, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}: - View.row.k(id, title, Bool.pick(String, snoozed, U.Time.ago(U.Side.until(st, id), now), U.Time.ago(now, a)), SB.Subs.badge(SB.Subs.count(st, id)), M.Phase.name(U.Side.phase(st, t)), sel, movable) + View.row.k(id, U.Label.title(st, id, title), Bool.pick(String, snoozed, U.Time.ago(U.Side.until(st, id), now), U.Time.ago(now, a)), SB.Subs.badge(SB.Subs.count(st, id)), M.Phase.name(U.Side.phase(st, t)), sel, movable) def View.rows_in(ts: List<&2, M.Thread>, +st: M.State, +sel: String, +now: Nat, +movable: Bool, +snoozed: Bool) -> List<&2, D.Node>: match ts: @@ -147,7 +147,7 @@ def View.project(r: U.SRow, +quiet: Bool, +open: Map<&2, Bool>, +st: M.State, +s case U.SRow{_, M.Project{+id, title, root, _, _}, M.Shelves{pinned, active, settled, hidden, snoozed}}: D.El{"section", id, [D.At{"class", Bool.pick(String, quiet, "project quiet", "project")}, D.At{"style", "--tint:" ++ BG.Badge.css_of(st, id)}, D.At{"data-drag-to", id}, D.At{"data-drag-act", "reorder-project"}], List.concat(&2, D.Node, [[D.El{"div", "", [D.At{"class", "project-head"}, D.At{"title", root}, D.At{"draggable", "true"}, D.At{"data-drag", id}, D.At{"data-drag-act", "reorder-project"}, D.At{"data-value", id}, D.On{"contextmenu", "proj-menu"}], - [WI.WIcon.button("badge", "look", id, "Colour and icon", BG.Badge.icon(st, id), 14, ""), D.Dom.span("name", title), WI.WIcon.button("icon", "new-thread", id, "New thread", "plus", 16, "")]}], + [WI.WIcon.button("badge", "look", id, "Colour and icon", BG.Badge.icon(st, id), 14, ""), D.Dom.span("name", U.Label.title(st, id, title)), WI.WIcon.button("icon", "new-thread", id, "New thread", "plus", 16, "")]}], View.look(String.eq(look, id), st, id, String.eq(rm, id)), View.rows_in(pinned, st, sel, now, True{}, False{}), View.rows_in(active, st, sel, now, True{}, False{}), @@ -202,7 +202,7 @@ def View.arow(r: U.ARow, +st: M.State, +sel: String) -> D.Node: case U.ARow{_, _, +t, +pid, +pt, +faded}: D.El{"div", M.Thread.id(t), [D.At{"class", Bool.pick(String, String.eq(M.Thread.id(t), sel), "thread on", Bool.pick(String, faded, "thread faded", "thread"))}, D.At{"style", "--tint:" ++ BG.Badge.css_of(st, pid)}, D.At{"data-value", M.Thread.id(t)}, D.On{"click", "select"}, D.On{"contextmenu", "row-menu"}], - [D.Dom.span("dot " ++ M.Phase.name(U.Side.phase(st, t)), ""), D.Dom.span("title", U.Active.title(t)), View.note(SB.Subs.badge(SB.Subs.count(st, M.Thread.id(t))), pt)]} + [D.Dom.span("dot " ++ M.Phase.name(U.Side.phase(st, t)), ""), D.Dom.span("title", U.Label.title(st, M.Thread.id(t), U.Active.title(t))), View.note(SB.Subs.badge(SB.Subs.count(st, M.Thread.id(t))), pt)]} def View.arows(rs: List<&2, U.ARow>, +st: M.State, +sel: String) -> List<&2, D.Node>: match rs: @@ -428,7 +428,7 @@ def View.header(t: M.Thread, +st: M.State, +panel: Bool, snoozed: Bool, picking: D.El{"header", "", [D.At{"class", "thread-head"}, D.At{"style", "--tint:" ++ BG.Badge.css_of(st, p)}], [ D.Dom.button("back", "select", "", "Threads", "‹"), D.El{"span", "", [D.At{"class", "badge"}], [WI.WIcon.svg(BG.Badge.icon(st, p), 16)]}, - D.Dom.span("title", title), + D.Dom.span("title", U.Label.title(st, id, title)), D.Dom.span("meta", Bool.pick(String, String.is_empty(branch), "", branch)), D.El{"div", "", [D.At{"class", "tools"}], List.append(&2, D.Node, View.tools(TO.Tools.head(TO.Head{View.running(turn), TO.Tools.settled(override, Nat.is_gt(sa, ua)), pinned, snoozed, archived, del, @@ -1109,16 +1109,20 @@ def View.thread.h(+u: U.Ui, t: M.Thread, hd: List<&2, D.Node>) -> D.Node: def View.thread(+u: U.Ui, t: M.Thread) -> D.Node: View.thread.h(u, t, Nil{}) -# a bot: its header and tabs, then its thread (Chat) or another tab -def View.bot.k(chat: Bool, +u: U.Ui, +b: BT.Bot, +tab: String) -> D.Node: +# a bot, here or mirrored from a linked machine: its header and tabs +# (BU.head), then its thread (Chat) or another tab +def View.bot.k(chat: Bool, +u: U.Ui, +b: BT.Bot, +hd: BI.Head) -> D.Node: match chat: case True{}: - View.thread.h(u, U.Ui.thread(U.Ui.state(u), BT.Bot.thread(b)), [WB.WB.head(u, b), WB.WB.tabs(tab)]) + View.thread.h(u, U.Ui.thread(U.Ui.state(u), BT.Bot.thread(b)), [WB.WB.head(u, b, hd), WB.WB.tabs(hd)]) case False{}: - D.El{"main", "bot", [D.At{"class", "thread-view bot-view"}], [WB.WB.head(u, b), WB.WB.tabs(tab), WB.WB.body(u, b, tab)]} + D.El{"main", "bot", [D.At{"class", "thread-view bot-view"}], [WB.WB.head(u, b, hd), WB.WB.tabs(hd), WB.WB.body(u, b, BI.Head.tab(hd))]} + +def View.bot.h(+u: U.Ui, +b: BT.Bot, +hd: BI.Head) -> D.Node: + View.bot.k(String.eq(BI.Head.tab(hd), "chat"), u, b, hd) def View.bot(+u: U.Ui, +b: BT.Bot) -> D.Node: - View.bot.k(String.eq(BI.BU.tab(u), "chat"), u, b, BI.BU.tab(u)) + View.bot.h(u, b, BI.BU.head.bot(u, b)) def View.main.s(settings: Bool, none: Bool, u: U.Ui, +sel: String, +n: Nat) -> D.Node: match settings none u: diff --git a/test/botdir_test.bend b/test/botdir_test.bend new file mode 100644 index 0000000000..1e732bfd87 --- /dev/null +++ b/test/botdir_test.bend @@ -0,0 +1,42 @@ +import Base +import ./check.bend as C +import ../src/core/bothub.bend as D +import ../src/core/bot.bend as B +import ../src/core/json.bend as J +import ../src/server/botnet.bend as BN + +def rows() -> List<&2, J.Json>: + [D.Dir.item("a", "alpha", 7n, "miso", "work", 42n), D.Dir.item("b", "beta", 3n, "nori", "ready", 40n)] + +def summary(xs: List<&2, J.Json>) -> String: + match xs: + case Nil{}: + "" + case +o <> t: + D.Dir.key(o) ++ ":" ++ J.Json.get_str(o, "state", "") ++ ":" ++ J.Json.as_raw(J.Json.get(o, "seen"), "") ++ ";" ++ summary(t) + +def work(w: D.DirWork) -> String: + match w: + case D.DirWork{a, q}: + String.join(a, ",") ++ "|" ++ String.join(q, ",") + +def main() -> IO(Unit): + do IO: + C.Check.str("failure changes only its peer and preserves seen", summary(D.Dir.merge(None{}, rows(), "a")), "a:miso:away:42;b:nori:ready:40;") + C.Check.str("success replaces all old rows for one peer", summary(D.Dir.merge(Some{[D.Dir.item("a", "alpha", 1n, "new", "ready", 50n)]}, rows(), "a")), "a:new:ready:50;b:nori:ready:40;") + C.Check.str("empty success removes only that peer", summary(D.Dir.merge(Some{Nil{}}, rows(), "a")), "b:nori:ready:40;") + C.Check.str("late result after URL change is ignored", summary(D.Dir.accept(False{}, Some{Nil{}}, rows(), "a")), summary(rows())) + C.Check.str("cache rows are away, ordered, and unlinked peers dropped", summary(D.Dir.cached(rows(), [B.Peer{"a", "alpha", "http://a", 0n, False{}}])), "a:miso:away:42;") + C.Check.str("cache peer IDs never match another peer name", summary(D.Dir.cached(rows(), [B.Peer{"c", "a", "http://c", 0n, False{}}])), "") + C.Check.str("bot order independent of arrival", summary(D.Dir.merge(Some{[D.Dir.item("a", "alpha", 0n, "z", "ready", 5n), D.Dir.item("a", "alpha", 0n, "a", "ready", 5n)]}, rows(), "a")), "a:a:ready:5;a:z:ready:5;b:nori:ready:40;") + C.Check.str("valid object is a failed directory", J.Json.show(J.Json.bool(Maybe.is_none(&2, List<&2, J.Json>, BN.Bn.fetched(BN.Resp{200n, "{}"}, "a", "alpha", 50n)))), "true") + C.Check.str("malformed JSON is a failed directory", J.Json.show(J.Json.bool(Maybe.is_none(&2, List<&2, J.Json>, BN.Bn.fetched(BN.Resp{200n, "["}, "a", "alpha", 50n)))), "true") + C.Check.str("HTTP failure is a failed directory", J.Json.show(J.Json.bool(Maybe.is_none(&2, List<&2, J.Json>, BN.Bn.fetched(BN.Resp{401n, "[]"}, "a", "alpha", 50n)))), "true") + C.Check.str("directory timeout is separate from delivery", String.join(BN.Bn.dir.args("http://a"), " "), "-sS --proto =http,https --connect-timeout 2 -m 4 --max-filesize 1048576 -X GET -H @- -w \n%{http_code} --url http://a") + C.Check.str("delivery retains its timeout", J.Json.show(J.Json.bool(List.contains(~String, ~String.eq, BN.Bn.args(True{}, "file", "http://a"), "15"))), "true") + C.Check.str("active peer cannot be queued twice", work(D.DirWork.add(D.DirWork{["a"], ["b"]}, "a")), "a|b") + C.Check.str("pending peer cannot be queued twice", work(D.DirWork.add(D.DirWork{["a"], ["b"]}, "b")), "a|b") + C.Check.str("four requests at most", work(D.DirPlan.work(D.DirWork.plan(D.DirWork{[], ["a", "b", "c", "d", "e"]}))), "d,c,b,a|e") + C.Check.str("completion fills slot without waiting for others", String.join(D.DirPlan.launch(D.DirWork.plan(D.DirWork.finish(D.DirWork{["d", "c", "b", "a"], ["e"]}, "b"))), ","), "e") + C.Check.str("empty directories cannot trigger a request loop", J.Json.show(J.Json.bool(D.Dir.stale(Nil{}, "a"))), "false") + C.Check.str("a discovered returning peer refreshes at once", Nat.show(List.length(&2, B.Peer, D.Dir.returned([B.Peer{"a", "alpha", "http://a", 0n, False{}}], Nil{}, [J.Json.obj([J.KV{"url", J.Str{"http://a"}}])]))), "1") diff --git a/test/botnet_test.bend b/test/botnet_test.bend index 14958c961f..8e8b3f6749 100644 --- a/test/botnet_test.bend +++ b/test/botnet_test.bend @@ -3,6 +3,7 @@ import ./check.bend as C import ../src/server/botnet.bend as BN import ../src/core/sign.bend as G import ../src/core/json.bend as J +import ../src/core/bothub.bend as BH import ../src/server/proc.bend as P # The pure parts of src/server/botnet.bend: which ids may name a secret @@ -103,8 +104,8 @@ def main() -> IO(Unit): C.Check.str("resp", resp(BN.Bn.resp.k(List.reverse(&2, String, String.lines("{\"name\":\"a\"}\n200")))), "200 {\"name\":\"a\"}") C.Check.str("resp failed", resp(BN.Bn.resp(P.Ran{7, "x"})), "0 ") C.Check.str("dir items", names(BN.Bn.items(J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse("[{\"name\":\"miso\",\"state\":\"ready\"},{\"name\":\"\"},{\"name\":\"a@b\",\"state\":\"work\"}]"), J.Null{})), "p1", "desk", 5n)), "miso/ready,ab/work") - C.Check.str("dir away", names(BN.Bn.fetched(BN.Resp{0n, ""}, "p1", "desk", 9n, - BN.Bn.items(J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse("[{\"name\":\"miso\",\"state\":\"ready\"}]"), J.Null{})), "p1", "desk", 5n))), "miso/away") - C.Check.str("dir away other peer", names(BN.Bn.fetched(BN.Resp{0n, ""}, "p2", "lap", 9n, - BN.Bn.items(J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse("[{\"name\":\"miso\",\"state\":\"ready\"}]"), J.Null{})), "p1", "desk", 5n))), "") - C.Check.str("dir fresh", names(BN.Bn.fetched(BN.Resp{200n, "[{\"name\":\"nori\",\"state\":\"idle\"}]"}, "p1", "desk", 9n, Nil{})), "nori/idle") + C.Check.str("dir away", names(BH.Dir.merge(BN.Bn.fetched(BN.Resp{0n, ""}, "p1", "desk", 9n), + BN.Bn.items(J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse("[{\"name\":\"miso\",\"state\":\"ready\"}]"), J.Null{})), "p1", "desk", 5n), "p1")), "miso/away") + C.Check.str("dir away other peer", names(BH.Dir.merge(BN.Bn.fetched(BN.Resp{0n, ""}, "p2", "lap", 9n), + BN.Bn.items(J.Json.elems(Maybe.default(&2, J.Json, J.Json.parse("[{\"name\":\"miso\",\"state\":\"ready\"}]"), J.Null{})), "p1", "desk", 5n), "p2")), "miso/ready") + C.Check.str("dir fresh", names(BH.Dir.merge(BN.Bn.fetched(BN.Resp{200n, "[{\"name\":\"nori\",\"state\":\"idle\"}]"}, "p1", "desk", 9n), Nil{}, "p1")), "nori/idle") diff --git a/test/far_test.bend b/test/far_test.bend new file mode 100644 index 0000000000..56522c84d7 --- /dev/null +++ b/test/far_test.bend @@ -0,0 +1,219 @@ +import Base +import ./check.bend as C +import ../src/core/model.bend as M +import ../src/core/codec.bend as K +import ../src/core/json.bend as J +import ../src/core/client.bend as U +import ../src/core/botui.bend as BU +import ../src/core/bot.bend as BT +import ../src/core/far.bend as FR +import ../src/mobile/hubs.bend as H +import ../src/mobile/screen.bend as MS + +# Threads elsewhere (far.bend): a hub's clients fold a linked machine's +# bots and threads in under ids of their own, once, in order, and send +# what they do to them back to that machine. + +def items(cs: List<&2, M.Change>) -> List<&2, J.Json>: + match cs: + case Nil{}: + Nil{} + case c <> t: + K.Change.encode(c) <> items(t) + +def log(+since: Nat, cs: List<&2, M.Change>) -> J.Json: + J.Json.obj([J.KV{"t", J.Json.str("log")}, J.KV{"since", J.Json.nat(since)}, J.KV{"items", J.Json.arr(items(cs))}]) + +def info(+remote: String) -> J.Json: + J.Json.obj([J.KV{"t", J.Json.str("info")}, J.KV{"info", J.Json.obj([J.KV{"bots.remote", J.Json.str(remote)}])}]) + +# items numbered from n, as the owning hub numbers its log +def numbered(cs: List<&2, M.Change>, +n: Nat) -> List<&2, FR.Item>: + match cs: + case Nil{}: + Nil{} + case c <> t: + FR.Item{n, K.Change.wire(c)} <> numbered(t, 1n+n) + +def far(+since: Nat, +head: Nat, away: Bool, xs: List<&2, FR.Item>) -> J.Json: + Maybe.default(&2, J.Json, J.Json.parse(FR.Fr.msg("p1", "box", away, since, head, xs)), J.Null{}) + +# box's Kit: its thread (no project), a message, a turn that ran +def box() -> List<&2, FR.Item>: + numbered([M.ThreadCreated{"bt1", "", "Kit", M.Local{}, "", "", "claude", "", 100n}, + M.BotSet{"b1", "Kit", "bt1", 7n, "helps", 100n}, + M.MessagePosted{"bt1", "m1", M.User{}, "hello", 101n}, + M.TurnChanged{"bt1", M.Running{}, 102n}], 11n) + +def more() -> List<&2, FR.Item>: + numbered([M.MessagePosted{"bt1", "m2", M.Assistant{}, "hi there", 103n}, M.TurnChanged{"bt1", M.Completed{}, 104n}], 20n) + +def here() -> List<&2, M.Change>: + [M.PeerSet{"p1", "box", "http://box:3787", False{}, 50n}, + M.ThreadCreated{"t1", "", "Pip", M.Local{}, "", "", "claude", "", 90n}, M.BotSet{"b9", "Pip", "t1", 3n, "", 90n}] + +def recv(+u: U.Ui, +o: J.Json) -> U.Ui: + U.Ui.recv(u, o) + +def joined() -> U.Ui: + recv(recv(recv(U.Ui.init(1000n), log(0n, here())), info("[{\"name\":\"Kit\",\"peer\":\"p1\",\"peerName\":\"box\",\"look\":7,\"state\":\"working\"}]")), + far(0n, 14n, False{}, box())) + +def title(t: M.Thread) -> String: + match t: + case M.Thread{_, _, ti, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}: + ti + +def count(+u: U.Ui, +th: String) -> String: + Nat.show(List.length(&2, M.Entry, M.State.mine(U.Ui.state(u), th))) + +def rows(rs: List<&2, BU.BRow>) -> String: + match rs: + case Nil{}: + "" + case BU.BRow{+i, +n, _, _, +m, _, _, r, +p, +l} <> t: + i ++ "|" ++ n ++ "|" ++ m ++ "|" ++ Bool.show(r) ++ "|" ++ p ++ "|" ++ l ++ ";" ++ rows(t) + +def acts(rs: List<&2, BU.BRow>) -> String: + match rs: + case Nil{}: + "" + case +r <> t: + BU.BU.act(r) ++ ";" ++ acts(t) + +def act_ui(a: U.Act) -> U.Ui: + match a: + case U.Act{u, _}: + u + +def route(m: Maybe<&2, FR.Route>) -> String: + match m: + case Some{FR.Route{l, mm, b}}: + l ++ " " ++ mm ++ " " ++ b + case None{}: + "none" + +def rpc(+m: String, +p: List<&2, J.KV>) -> J.Json: + J.Json.obj([J.KV{"id", J.Num{"7"}}, J.KV{"m", J.Str{m}}, J.KV{"p", J.Json.obj(p)}]) + +# the owning hub's state: Kit's thread and a project's thread +def owner() -> M.State: + M.State.replay([M.ProjectCreated{"p", "board", "/b", 1n}, M.ThreadCreated{"pt", "p", "layout", M.Local{}, "", "", "claude", "", 2n}, + M.ThreadCreated{"bt1", "", "Kit", M.Local{}, "", "", "claude", "", 3n}, M.BotSet{"b1", "Kit", "bt1", 7n, "", 3n}, + M.AskOpened{"a1", "bt1", "tool", "Bash", "ls", "", 4n}, M.AskOpened{"a2", "pt", "tool", "Bash", "ls", "", 5n}], M.State.empty()) + +def project.items() -> List<&2, FR.Item>: + numbered([M.ProjectCreated{"p", "Board", "/board", 1n}, M.ThreadCreated{"pt", "p", "Layout", M.Local{}, "", "", "claude", "", 2n}, + M.MessagePosted{"pt", "m", M.User{}, "shared work", 3n}, M.AskOpened{"pa", "pt", "approval", "Bash", "ls", "", 4n}], 15n) + +def phone() -> H.Hubs: + H.Hubs.after(H.Hubs.recv(H.Hubs.after(H.Hubs.recv(H.Hubs.set(H.Hubs.init(1000n, "c"), ["here:3791", "box:3792"]), "here:3791", + log(0n, [M.PeerSet{"p1", "box", "http://box:3792", False{}, 1n}]))), "here:3791", far(0n, 18n, False{}, project.items()))) + +def paired(+h: H.Hubs) -> H.Hubs: + H.Hubs.after(H.Hubs.recv(h, "box:3792", log(0n, [M.PeerSet{"p1", "here", "http://here:3791", False{}, 1n}, + M.ProjectCreated{"p", "Board", "/board", 1n}, M.ThreadCreated{"pt", "p", "Layout", M.Local{}, "", "", "claude", "", 2n}]))) + +def settled.phone() -> H.Hubs: + H.Hubs.after(H.Hubs.recv(H.Hubs.after(H.Hubs.recv(paired(phone()), "box:3792", log(3n, [M.ThreadSettled{"pt", True{}, 995n}]))), + "here:3791", far(18n, 19n, False{}, numbered([M.ThreadSettled{"pt", True{}, 995n}], 19n)))) + +def first(+o: J.Json, +k: String) -> J.Json: + Maybe.default(&2, J.Json, List.head(&2, J.Json, J.Json.elems(J.Json.get_obj(o, k))), J.Null{}) + +def fields(xs: List<&2, J.Json>, +k: String) -> String: + match xs: + case Nil{}: + "" + case x <> t: + J.Json.get_str(x, k, "") ++ ";" ++ fields(t, k) + +def main() -> IO(Unit): + do IO: + +u : U.Ui = joined() + C.Check.str("ids", FR.Fr.id("p1", "t~x") ++ " " ++ FR.Fr.link("p1~t~x") ++ " " ++ FR.Fr.local("p1~t~x") ++ " [" ++ FR.Fr.link("t1") ++ "] " ++ FR.Fr.local("t1"), + "p1~t~x p1 t~x [] t1") + C.Check.str("thread folded in under its link", count(u, "p1~bt1") ++ " " ++ title(U.Ui.thread(U.Ui.state(u), "p1~bt1")), "1 Kit") + C.Check.str("its hub's sequence untouched", Nat.show(M.State.seq(U.Ui.state(u))), "3") + C.Check.str("bot listed once, as a bot with its machine", rows(BU.BU.rows(u)), + "b9|Pip|sleep|False||;p1~b1|Kit|work|True|box|p1;") + C.Check.str("rows open as bots", acts(BU.BU.rows(u)), "bot;bot;") + C.Check.str("title with its machine", U.Ui.far.title(u, "p1~bt1", "Kit") ++ "/" ++ U.Ui.far.title(u, "t1", "Pip"), "Kit · box/Pip") + +pu : U.Ui = recv(u, far(14n, 18n, False{}, project.items())) + C.Check.str("ordinary project thread folded with its parent", U.Ui.thread_project(U.Ui.thread(U.Ui.state(pu), "p1~pt")) ++ " " ++ count(pu, "p1~pt"), "p1~p 1") + C.Check.str("ordinary thread marker", U.Label.title(U.Ui.state(pu), "p1~pt", "Layout"), "Layout · box") + C.Check.str("far composer draft survives saving", Bool.show(U.Draft.safe("p1~pt")) ++ " " ++ Bool.show(U.Draft.safe("p1~../pt")), "True False") + C.Check.str("shared approval id", Nat.show(List.length(&2, M.Ask, U.Asks.open(U.Ui.state(pu), "p1~pt"))), "1") + C.Check.str("phone project marker", J.Json.get_str(first(H.Hubs.screen(phone()), "projects"), "title", ""), "Board · box") + C.Check.str("phone thread marker", J.Json.get_str(first(first(H.Hubs.screen(phone()), "projects"), "threads"), "title", ""), "Layout · box") + C.Check.str("phone keeps mirror while owner catalog not installed", Nat.show(List.length(&2, J.Json, J.Json.elems(J.Json.get_obj(H.Hubs.screen(phone()), "projects")))), "1") + C.Check.str("phone paired owner removes mirror project", fields(J.Json.elems(J.Json.get_obj(H.Hubs.screen(paired(phone())), "projects")), "id"), "box:3792|p;") + C.Check.str("paired owner's project keeps its machine marker", J.Json.get_str(first(H.Hubs.screen(paired(phone())), "projects"), "machine", ""), "box") + +active : H.Hubs = H.Hubs.after(H.Hubs.act(paired(phone()), "setting", "sidebar.view=active")) + C.Check.str("phone active has one ordinary thread", fields(J.Json.elems(J.Json.get_obj(H.Hubs.screen(active), "active")), "id"), "box:3792|pt;") + +settled : H.Hubs = H.Hubs.after(H.Hubs.act(settled.phone(), "setting", "sidebar.view=active")) + C.Check.str("phone counts hidden settled thread once", Nat.show(J.Json.get_nat(J.Json.get_obj(H.Hubs.screen(settled), "settled"), "count", 0n)), "1") + +older : H.Hubs = H.Hubs.after(H.Hubs.tick(settled, 1000000n)) + C.Check.str("phone counts hidden older project once", Nat.show(J.Json.get_nat(J.Json.get_obj(H.Hubs.screen(older), "older"), "count", 0n)), "1") + +bp : H.Hubs = H.Hubs.after(H.Hubs.recv(H.Hubs.after(H.Hubs.recv(H.Hubs.set(H.Hubs.init(1000n, "c"), ["here:3791", "box:3792"]), + "here:3791", log(0n, [M.PeerSet{"p1", "box", "http://box:3792", False{}, 1n}]))), "here:3791", far(0n, 14n, False{}, box()))) + C.Check.str("phone keeps bot until paired owner installs it", Nat.show(List.length(&2, J.Json, J.Json.elems(J.Json.get_obj(H.Hubs.screen(bp), "bots")))), "1") + +bp2 : H.Hubs = H.Hubs.after(H.Hubs.recv(bp, "box:3792", log(0n, [M.PeerSet{"p1", "here", "http://here:3791", False{}, 1n}, + M.ThreadCreated{"bt1", "", "Kit", M.Local{}, "", "", "claude", "", 100n}, M.BotSet{"b1", "Kit", "bt1", 7n, "helps", 100n}]))) + C.Check.str("phone paired owner lists bot once", Nat.show(List.length(&2, J.Json, J.Json.elems(J.Json.get_obj(H.Hubs.screen(bp2), "bots")))), "1") + # the report of it (Kit@box) opens the mirrored bot, not a hub switch + +s : U.Ui = act_ui(U.Ui.act(u, "remote", "Kit@box")) + C.Check.str("remote pick opens the mirror", U.Ui.scratch(s, "@bot") ++ " " ++ U.Ui.sel(s) ++ " [" ++ U.Ui.scratch(s, "@remote") ++ "] [" ++ U.Ui.info(s, "local.machine") ++ "]", + "p1~b1 p1~bt1 [] []") + # the same batch again, and an older one, change nothing + +u2 : U.Ui = recv(recv(u, far(0n, 14n, False{}, box())), far(10n, 13n, False{}, box())) + C.Check.str("a batch twice is taken once", count(u2, "p1~bt1"), "1") + # a batch past a gap is left for the pull + +u3 : U.Ui = recv(u2, far(18n, 21n, False{}, more())) + C.Check.str("a gap takes nothing", count(u3, "p1~bt1") ++ " " ++ Nat.show(FR.Fr.head(U.Ui.state(u3), "p1")), "1 14") + +u4 : U.Ui = recv(u3, far(14n, 21n, False{}, more())) + C.Check.str("the next batch in order", count(u4, "p1~bt1") ++ " " ++ Nat.show(FR.Fr.head(U.Ui.state(u4), "p1")), "2 21") + # A new authoritative history replaces only its projection. Same + # epoch on another snapshot remains idempotent, including after load. + +e1 : U.Ui = recv(u4, Maybe.default(&2, J.Json, J.Json.parse(FR.Fr.msg.epoch("p1", "box", False{}, 0n, 14n, box(), "old")), J.Null{})) + +e2 : U.Ui = recv(e1, Maybe.default(&2, J.Json, J.Json.parse(FR.Fr.msg.epoch("p2", "other", False{}, 0n, 14n, box(), "other")), J.Null{})) + +e3 : U.Ui = recv(e2, Maybe.default(&2, J.Json, J.Json.parse(FR.Fr.msg.epoch("p1", "box", False{}, 0n, 1n, + numbered([M.ThreadCreated{"new", "", "New history", M.Local{}, "", "", "claude", "", 200n}], 1n), "new")), J.Null{})) + C.Check.str("reset removes old entries and bot", count(e3, "p1~bt1") ++ " " ++ Bool.show(BT.Maybe.is(BT.Bot, BT.World.find.id(BT.World.bot(M.State.bots(U.Ui.state(e3)), "p1~b1")))), "0 False") + C.Check.str("reset preserves local and other mirror", Nat.show(M.State.seq(U.Ui.state(e3))) ++ " " ++ count(e3, "p2~bt1") ++ " " ++ title(U.Ui.thread(U.Ui.state(e3), "t1")), "3 1 Pip") + C.Check.str("reset takes the lower new head", Nat.show(FR.Fr.head(U.Ui.state(e3), "p1")) ++ " " ++ title(U.Ui.thread(U.Ui.state(e3), "p1~new")), "1 New history") + +disk : FR.In = FR.Fr.file.read("{\"epoch\":\"new\"}\n" ++ FR.Fr.file.batch(numbered([M.ThreadCreated{"new", "", "New history", M.Local{}, "", "", "claude", "", 200n}], 1n), 1n), "p1", "box") + C.Check.str("disk keeps epoch and head", FR.In.epoch(disk) ++ " " ++ Nat.show(FR.In.head(disk)) ++ " " ++ Bool.show(FR.In.away(disk)), "new 1 True") + C.Check.str("late disk load cannot replace live batch", Nat.show(FR.In.head(FR.In.loaded(FR.In.put(FR.In.new("p1", "box"), 0n, 21n, more()), disk))), "21") + C.Check.str("reply preserves owner's refusal and request id", FR.Fr.reply(J.Json.obj([J.KV{"t", J.Str{"reply"}}, J.KV{"id", J.Num{"0"}}, J.KV{"ok", J.Flag{False{}}}, J.KV{"error", J.Str{"cannot start"}}]), "23"), + "{\"t\":\"reply\",\"id\":23,\"ok\":false,\"error\":\"cannot start\"}") + # its machine stops answering: kept, away + +u5 : U.Ui = recv(u4, far(21n, 21n, True{}, Nil{})) + C.Check.str("away keeps it", count(u5, "p1~bt1") ++ " " ++ rows(BU.BU.rows(u5)) ++ " " ++ U.Ui.far.title(u5, "p1~bt1", "Kit"), + "2 b9|Pip|sleep|False||;p1~b1|Kit|away|True|box|p1; Kit · box (away)") + # a reconnect resumes this hub's log: the mirror stays; a fresh log + # forgets it with its head, and the snapshot brings it back + +u6 : U.Ui = recv(u5, log(3n, Nil{})) + C.Check.str("resume keeps it", count(u6, "p1~bt1"), "2") + +u7 : U.Ui = recv(u6, log(0n, here())) + C.Check.str("fresh log forgets it", count(u7, "p1~bt1") ++ " " ++ Nat.show(FR.Fr.head(U.Ui.state(u7), "p1")), "0 0") + +u8 : U.Ui = recv(u7, far(0n, 21n, False{}, List.append(&2, FR.Item, box(), more()))) + C.Check.str("snapshot again", count(u8, "p1~bt1"), "2") + # requests: one about a thread elsewhere goes to its machine in its ids + C.Check.str("route turn.start", route(FR.Fr.route(rpc("turn.start", [J.KV{"thread", J.Str{"p1~bt1"}}, J.KV{"text", J.Str{"hi"}}, J.KV{"msg", J.Str{"m9"}}]))), + "p1 turn.start {\"id\":0,\"m\":\"turn.start\",\"p\":{\"thread\":\"bt1\",\"text\":\"hi\",\"msg\":\"m9\"}}") + C.Check.str("route ask.answer", route(FR.Fr.route(rpc("ask.answer", [J.KV{"id", J.Str{"p1~a1"}}, J.KV{"answer", J.Str{"yes"}}]))), + "p1 ask.answer {\"id\":0,\"m\":\"ask.answer\",\"p\":{\"id\":\"a1\",\"answer\":\"yes\"}}") + C.Check.str("a request here stays", route(FR.Fr.route(rpc("turn.start", [J.KV{"thread", J.Str{"bt1"}}, J.KV{"text", J.Str{"a~b"}}]))), "none") + # the owning hub: what it shares, and what it lets a linked machine do + +st : M.State = owner() + C.Check.str("shared", Bool.show(FR.Fr.shared(st, M.MessagePosted{"bt1", "m", M.User{}, "x", 9n})) ++ " " ++ Bool.show(FR.Fr.shared(st, M.MessagePosted{"pt", "m", M.User{}, "x", 9n})) + ++ " " ++ Bool.show(FR.Fr.shared(st, M.SettingSet{"k", "v", 9n})) ++ " " ++ Bool.show(FR.Fr.shared(st, M.AskClosed{"a1", "y", 9n})) ++ " " ++ Bool.show(FR.Fr.shared(st, M.AskClosed{"a2", "y", 9n})), + "True True False True True") + C.Check.str("allowed", Bool.show(FR.Fr.allowed(st, rpc("turn.start", [J.KV{"thread", J.Str{"bt1"}}]))) ++ " " ++ Bool.show(FR.Fr.allowed(st, rpc("turn.start", [J.KV{"thread", J.Str{"pt"}}]))) + ++ " " ++ Bool.show(FR.Fr.allowed(st, rpc("ask.answer", [J.KV{"id", J.Str{"a1"}}]))) ++ " " ++ Bool.show(FR.Fr.allowed(st, rpc("ask.answer", [J.KV{"id", J.Str{"a2"}}]))) + ++ " " ++ Bool.show(FR.Fr.allowed(st, rpc("setting.set", [J.KV{"key", J.Str{"x"}}]))) ++ " " ++ Bool.show(FR.Fr.allowed(st, rpc("thread.delete", [J.KV{"thread", J.Str{"bt1"}}]))), + "True True True True False False") + # a mirrored bot's space page, served by the mirroring hub + +pi : FR.In = FR.In.put(FR.In.new("p1", "box"), 0n, 3n, numbered([M.SpacePageSet{"b1", "

old

", 1n}, M.SpacePageSet{"b2", "

other

", 2n}, M.SpacePageSet{"b1", "

new

", 3n}], 1n)) + C.Check.str("mirrored space page", FR.In.page(pi, "b1") ++ "|" ++ FR.In.page(pi, "b9"), "

new

|") diff --git a/test/farui_test.bend b/test/farui_test.bend new file mode 100644 index 0000000000..381ea52e27 --- /dev/null +++ b/test/farui_test.bend @@ -0,0 +1,177 @@ +import Base +import ./check.bend as C +import ../src/core/model.bend as M +import ../src/core/codec.bend as K +import ../src/core/json.bend as J +import ../src/core/client.bend as U +import ../src/core/botui.bend as BU +import ../src/core/far.bend as FR +import ../src/app/nui.bend as N +import ../src/core/bot.bend as BT +import ../src/mobile/hubs.bend as H + +# Bots elsewhere in every client's one bot view (botui.bend's BU.head): +# a mirrored bot opens as a bot with its machine beside its name and the +# tabs its machine shares; a bot only reported by a linked machine is the +# same view with its chat alone; a phone paired with a bot's own hub and +# a hub that mirrors it shows the bot once, as its own hub's. + +def items(cs: List<&2, M.Change>) -> List<&2, J.Json>: + match cs: + case Nil{}: + Nil{} + case c <> t: + K.Change.encode(c) <> items(t) + +def log(cs: List<&2, M.Change>) -> J.Json: + J.Json.obj([J.KV{"t", J.Json.str("log")}, J.KV{"since", J.Json.nat(0n)}, J.KV{"items", J.Json.arr(items(cs))}]) + +def info(+remote: String) -> J.Json: + J.Json.obj([J.KV{"t", J.Json.str("info")}, J.KV{"info", J.Json.obj([J.KV{"bots.remote", J.Json.str(remote)}])}]) + +def numbered(cs: List<&2, M.Change>, +n: Nat) -> List<&2, FR.Item>: + match cs: + case Nil{}: + Nil{} + case c <> t: + FR.Item{n, K.Change.wire(c)} <> numbered(t, 1n+n) + +def far(away: Bool) -> J.Json: + Maybe.default(&2, J.Json, J.Json.parse(FR.Fr.msg("p1", "box", away, 0n, 12n, + numbered([M.ThreadCreated{"bt1", "", "Kit", M.Local{}, "", "", "claude", "", 100n}, M.BotSet{"b1", "Kit", "bt1", 7n, "helps", 100n}], 11n))), J.Null{}) + +# this hub (lap) is linked to box (p1); box shares Kit and reports Kit +# and Nori, whose threads it does not share +def here() -> List<&2, M.Change>: + [M.PeerSet{"p1", "box", "http://box:3787", False{}, 50n}, + M.ThreadCreated{"t1", "", "Pip", M.Local{}, "", "", "claude", "", 90n}, M.BotSet{"b9", "Pip", "t1", 3n, "", 90n}] + +def dir() -> String: + "[{\"name\":\"Kit\",\"peer\":\"p1\",\"peerName\":\"box\",\"look\":7,\"state\":\"ready\"},{\"name\":\"Nori\",\"peer\":\"p1\",\"peerName\":\"box\",\"look\":3,\"state\":\"working\"}]" + +def recv(+u: U.Ui, +o: J.Json) -> U.Ui: + U.Ui.recv(u, o) + +def joined() -> U.Ui: + recv(recv(recv(U.Ui.init(1000n), log(here())), info(dir())), far(False{})) + +def act_ui(a: U.Act) -> U.Ui: + match a: + case U.Act{u, _}: + u + +def go(+u: U.Ui, +a: String, +v: String) -> U.Ui: + act_ui(U.Ui.act(u, a, v)) + +def rid(r: BU.BRow) -> String: + match r: + case BU.BRow{i, _, _, _, _, _, _, _, _, _}: + i + +def rows(rs: List<&2, BU.BRow>) -> String: + match rs: + case Nil{}: + "" + case +r <> t: + rid(r) ++ ">" ++ BU.BU.act(r) ++ ">" ++ BU.BU.row.title(r) ++ ";" ++ rows(t) + +def strs(xs: List<&2, String>) -> String: + String.join(xs, ",") + +def head(+h: BU.Head) -> String: + BU.Head.title(h) ++ "|" ++ BU.Head.mood(h) ++ "|" ++ strs(BU.Head.tabs(h)) ++ "|" ++ BU.Head.tab(h) ++ "|" ++ Bool.show(BU.Head.viewer(h)) ++ "|" ++ Bool.show(BU.Head.owns(h)) + +def view(v: BU.View) -> String: + match v: + case BU.VThread{}: + "thread" + case BU.VBot{b}: + "bot " ++ BT.Bot.id(b) + case BU.VRoom{_}: + "room" + case BU.VRemote{id}: + "remote " ++ id + +# Phones: lap (the bot's own hub: Kit is b1 there) and box, which mirrors +# lap's bots over link p1 (the link's id is the same at both ends) +def lap() -> List<&2, M.Change>: + [M.PeerSet{"p1", "box", "http://box:3787", False{}, 50n}, + M.ThreadCreated{"bt1", "", "Kit", M.Local{}, "", "", "claude", "", 100n}, M.BotSet{"b1", "Kit", "bt1", 7n, "helps", 100n}] + +def box() -> List<&2, M.Change>: + [M.PeerSet{"p1", "lap", "http://lap:3787", False{}, 50n}, + M.ThreadCreated{"t2", "", "Nori", M.Local{}, "", "", "claude", "", 90n}, M.BotSet{"b2", "Nori", "t2", 3n, "", 90n}] + +def hub(+h: H.Hubs, +k: String, +o: J.Json) -> H.Hubs: + H.Hubs.after(H.Hubs.recv(h, k, o)) + +def mirror() -> J.Json: + Maybe.default(&2, J.Json, J.Json.parse(FR.Fr.msg("p1", "lap", False{}, 0n, 12n, + numbered([M.ThreadCreated{"bt1", "", "Kit", M.Local{}, "", "", "claude", "", 100n}, M.BotSet{"b1", "Kit", "bt1", 7n, "helps", 100n}], 11n))), J.Null{}) + +def paired(lapped: List<&2, M.Change>) -> H.Hubs: + hub(hub(hub(H.Hubs.set(H.Hubs.init(0n, "c"), ["lap:3787", "box:3787"]), "lap:3787", log(lapped)), "box:3787", log(box())), "box:3787", mirror()) + +def only_box() -> H.Hubs: + hub(hub(H.Hubs.set(H.Hubs.init(0n, "c"), ["box:3787"]), "box:3787", log(box())), "box:3787", mirror()) + +def jstrs(js: List<&2, J.Json>, +key: String) -> String: + match js: + case Nil{}: + "" + case j <> t: + J.Json.get_str(j, key, "") ++ ";" ++ jstrs(t, key) + +def jbools(js: List<&2, J.Json>, +key: String) -> String: + match js: + case Nil{}: + "" + case j <> t: + Bool.show(J.Json.get_bool(j, key, False{})) ++ ";" ++ jbools(t, key) + +def list(+h: H.Hubs, +key: String) -> String: + jstrs(J.Json.elems(J.Json.get_obj(H.Hubs.screen(h), "bots")), key) + +def pbot(+h: H.Hubs) -> J.Json: + J.Json.get_obj(H.Hubs.screen(h), "bot") + +def nori.arrives(+u: U.Ui) -> U.Ui: + U.Ui.recv(u, Maybe.default(&2, J.Json, J.Json.parse(FR.Fr.msg("p1", "box", False{}, 12n, 14n, + numbered([M.ThreadCreated{"nt", "", "Nori", M.Local{}, "", "", "claude", "", 103n}, M.BotSet{"nb", "Nori", "nt", 3n, "", 103n}], 13n))), J.Null{})) + +def main() -> IO(Unit): + do IO: + +u : U.Ui = joined() + C.Check.str("rows: mirrored opens as a bot, reported only as a conversation", rows(BU.BU.rows(u)), + "b9>bot>Pip;p1~b1>bot>Kit · box;Nori@box>remote>Nori · box;") + # the mirrored bot, chosen from its row or its report + +k : U.Ui = go(u, "bot", "p1~b1") + C.Check.str("mirrored bot shown as a bot", view(BU.BU.view(k)), "bot p1~b1") + C.Check.str("its header", head(BU.BU.head(k, BU.BU.view(k))), "Kit · box|idle|chat,space,memory,routines|chat|False|False") + C.Check.str("remote action opens the mirror", view(BU.BU.view(go(u, "remote", "Kit@box"))), "bot p1~b1") + C.Check.str("a tab it lacks falls back to chat", BU.BU.tab.of(go(k, "bot-tab", "browser"), "p1~b1") ++ " " ++ BU.BU.tab.of(go(k, "bot-tab", "memory"), "p1~b1"), "chat memory") + C.Check.str("a bot here keeps every tab", head(BU.BU.head(go(u, "bot", "b9"), BU.BU.view(go(u, "bot", "b9")))), "Pip|sleep|chat,space,browser,memory,routines,hooks,settings|chat|True|True") + C.Check.str("away while its machine is", head(BU.BU.head(recv(k, far(True{})), BU.BU.view(recv(k, far(True{}))))), "Kit · box (away)|away|chat,space,memory,routines|chat|False|False") + # a bot only reported: the bot view with its chat alone + +n : U.Ui = go(u, "remote", "Nori@box") + C.Check.str("reported only: the conversation", view(BU.BU.view(n)), "remote Nori@box") + +arrived : U.Ui = nori.arrives(n) + C.Check.str("reported choice adopts its arriving mirrored thread", U.Ui.scratch(arrived, "@bot") ++ " " ++ U.Ui.sel(arrived) ++ " [" ++ U.Ui.scratch(arrived, "@remote") ++ "]", "p1~nb p1~nt []") + C.Check.str("arriving thread keeps native keyboard focus", N.Nui.focus_of(N.Nui.unremote(N.Nui.with_focus(N.Nui.init(arrived, 1400, 900), N.Fld.alias("remote-send")))), "composer") + +writing : U.Ui = nori.arrives(go(n, "bfield", "rtext\u{1f}unfinished")) + C.Check.str("arrival preserves an unfinished composer", U.Bots.field(writing, "rtext") ++ " " ++ U.Ui.scratch(writing, "@remote"), "unfinished Nori@box") + C.Check.str("its header", head(BU.BU.head(n, BU.BU.view(n))), "Nori · box|work|chat|chat|False|False") + C.Check.str("one no machine lists", head(BU.BU.head(u, BU.VRemote{"Ghost@far"})), "Ghost@far|away|chat|chat|False|False") + # phones: the row's action and the bot's header + C.Check.str("phone rows act", list(only_box(), "act"), "bot;bot;") + C.Check.str("phone rows keep remote", jbools(J.Json.elems(J.Json.get_obj(H.Hubs.screen(only_box()), "bots")), "remote"), "False;True;") + C.Check.str("phone row titles", list(only_box(), "title"), "Nori;Kit · lap;") + +pb : H.Hubs = H.Hubs.after(H.Hubs.act(only_box(), "bot", "box:3787|p1~b1")) + C.Check.str("phone bot header", J.Json.get_str(pbot(pb), "title", "") ++ " " ++ jstrs(J.Json.elems(J.Json.get_obj(pbot(pb), "tabs")), "id") ++ " " ++ J.Json.show(J.Json.get_obj(pbot(pb), "edit")), + "Kit · lap chat;space;memory;routines; false") + +pr : H.Hubs = H.Hubs.after(H.Hubs.act(only_box(), "remote", "box:3787|Ghost@far")) + C.Check.str("phone reported bot header", J.Json.get_str(pbot(pr), "kind", "") ++ " " ++ J.Json.get_str(pbot(pr), "title", "") ++ " " ++ jstrs(J.Json.elems(J.Json.get_obj(pbot(pr), "tabs")), "id"), + "remote Ghost@far chat;") + # paired with both: Kit once, as lap's own + C.Check.str("paired with both: each bot once, as its own hub's", list(paired(lap()), "id"), "lap:3787|b1;box:3787|b2;") + C.Check.str("even when its hub has no bot of that name", list(paired([M.PeerSet{"p1", "box", "http://box:3787", False{}, 50n}]), "id"), "box:3787|b2;") diff --git a/test/hub_switch_test.bend b/test/hub_switch_test.bend new file mode 100644 index 0000000000..1b19002bd4 --- /dev/null +++ b/test/hub_switch_test.bend @@ -0,0 +1,98 @@ +import Base +import ./check.bend as C +import ../src/core/client.bend as U +import ../src/core/model.bend as M +import ../src/core/json.bend as J +import ../src/app/nui.bend as N + +# Duplicate names on two hubs must select the destination's bot even if +# an update arrives before its log. The same path covers cached switches, +# full adoption at home, and a missed BotSet arriving after a reply. +def json(s: String) -> J.Json: + Maybe.default(&2, J.Json, J.Json.parse(s), J.Null{}) + +def seed(+prefix: String) -> U.Ui: + U.Ui.info_set(U.Ui.fold([ + M.ThreadCreated{prefix ++ "-thread", "", "Kit", M.Local{}, "", "", "claude", "", 1n}, + M.BotSet{prefix ++ "-bot", "Kit", prefix ++ "-thread", 3n, "", 1n}], U.Ui.init(1n)), "origin", prefix ++ "-origin") + +def selected() -> U.Ui: + U.Act.ui(U.Bots.select(seed("local"), "local-bot")) + +def remote() -> String: + "http://localhost:3792" + +def transition(+u: U.Ui, +hub: String) -> U.Ui: + U.Act.ui(U.Far.open(False{}, u, hub, "Kit@other", "Kit")) + +def update() -> J.Json: + json("{\"t\":\"update\",\"state\":\"\"}") + +def remote_log() -> J.Json: + json("{\"t\":\"log\",\"since\":0,\"origin\":\"remote-origin\",\"info\":{},\"items\":[{\"$\":\"ThreadCreated\",\"id\":\"remote-thread\",\"title\":\"Kit\",\"env\":\"local\",\"provider\":\"claude\",\"at\":1},{\"$\":\"BotSet\",\"id\":\"remote-bot\",\"name\":\"Kit\",\"thread\":\"remote-thread\",\"look\":3,\"at\":1}]}") + +def empty_log() -> J.Json: + json("{\"t\":\"log\",\"since\":0,\"origin\":\"local-origin\",\"info\":{},\"items\":[]}") + +def snapshot(+hub: String, +prefix: String) -> U.HubView: + U.HubView{hub, U.Ui.state.of(seed(prefix)), U.Ui.infos(seed(prefix))} + +# Exported to the replay measurement harness: the same actual client +# action/update/log sequence used by the native and web hosts. +def replay() -> String: + U.Ui.sel(U.Ui.recv(U.Ui.recv(transition(selected(), remote()), update()), remote_log())) + +def main() -> IO(Unit): + +sw = transition(U.Ui.scratch_set(U.Ui.scratch_set(selected(), "@room", "old-room"), "@remote", "old@hub"), remote()) + +updated = U.Ui.recv(sw, update()) + +joined = U.Ui.recv(updated, remote_log()) + +home = N.Nui.adopt(N.Nui.init(transition(joined, ""), 800, 600), U.Ui.state.of(seed("local")), empty_log()) + +stale = U.Ui.info_set(U.Ui.info_set(U.Ui.adopt.raw(sw, U.Ui.state.of(seed("remote"))), "local.state.hub", remote()), "local.state.gen", "old-connection") + +wrong = U.Ui.scratch_set(U.Ui.recv(sw, remote_log()), "@bot.want", "Kit") + +wrong_hub = U.Ui.scratch_set(U.Ui.scratch_set(wrong, "@want.hub", "another-hub"), "@bot", "") + +cache = U.HubCache.put(snapshot("", "local"), U.HubCache.put(snapshot(remote(), "remote"), Nil{})) + +cached = U.Ui.cached(U.HubCache.get(cache, remote()), U.Ui.connection(sw, "remote#2")) + +delta = U.Ui.recv(cached, json("{\"t\":\"log\",\"since\":2,\"origin\":\"remote-origin\",\"info\":{},\"items\":[{\"$\":\"MessagePosted\",\"thread\":\"remote-thread\",\"id\":\"new-message\",\"role\":\"user\",\"text\":\"hello\",\"at\":2}]}")) + +many = U.HubCache.put(snapshot("third", "third"), cache) + +waiting = U.Ui.recv(U.Ui.connection(sw, "remote#3"), empty_log()) + +later = U.Ui.recv(waiting, json("{\"t\":\"changes\",\"items\":[{\"$\":\"ThreadCreated\",\"id\":\"remote-thread\",\"title\":\"Kit\",\"env\":\"local\",\"provider\":\"claude\",\"at\":1},{\"$\":\"BotSet\",\"id\":\"remote-bot\",\"name\":\"Kit\",\"thread\":\"remote-thread\",\"look\":3,\"at\":1}]}")) + +thread = U.Want.thread(U.Act.ui(U.Ui.machine(selected(), remote())), "remote-thread") + +with_view = U.Ui.recv(joined, json("{\"t\":\"view\",\"thread\":\"remote-thread\",\"kind\":\"board\",\"path\":\"/tmp/remote.kicad_pcb\"}")) + do IO: + C.Check.str("machine clears selection", U.Ui.sel(sw), "") + C.Check.str("machine clears bot", U.Ui.scratch(sw, "@bot"), "") + C.Check.str("machine clears room", U.Ui.scratch(sw, "@room"), "") + C.Check.str("machine clears remote", U.Ui.scratch(sw, "@remote"), "") + C.Check.str("machine clears old model", Nat.show(U.Ui.seq(sw)), "0") + C.Check.str("machine retains client identity", U.Ui.info(transition(U.Ui.set_cid(selected(), "window-client"), remote()), "cid"), "window-client") + C.Check.str("pre-log update cannot consume want", U.Ui.scratch(updated, "@bot.want"), "Kit") + C.Check.str("pre-log update cannot pick old bot", U.Ui.sel(updated), "") + C.Check.str("destination log resolves duplicate name", U.Ui.sel(joined), "remote-thread") + C.Check.str("destination bot selected", U.Ui.scratch(joined, "@bot"), "remote-bot") + C.Check.str("resolved want consumed", U.Ui.scratch(joined, "@bot.want"), "") + C.Check.str("home adoption resolves immediately", U.Ui.sel(N.Nui.ui(home)), "local-thread") + C.Check.str("home adoption selects correct bot", U.Ui.scratch(N.Nui.ui(home), "@bot"), "local-bot") + C.Check.str("stale generation cannot consume want", U.Ui.scratch(U.Bots.wanted(stale), "@bot.want"), "Kit") + C.Check.str("stale generation cannot select matching bot", U.Ui.sel(U.Bots.wanted(stale)), "") + C.Check.str("wrong hub cannot consume want", U.Ui.scratch(U.Bots.wanted(wrong_hub), "@bot.want"), "Kit") + C.Check.str("cached state displays before log", U.Ui.sel(cached), "remote-thread") + C.Check.str("cached sequence retained", Nat.show(U.Ui.seq(cached)), "2") + C.Check.str("cached origin retained", U.Ui.info(cached, "origin"), "remote-origin") + C.Check.str("cached pending rebinds to connection", U.Ui.scratch(cached, "@want.gen"), "remote#2") + C.Check.str("delta reconnect retains bot", U.Ui.sel(delta), "remote-thread") + C.Check.str("delta reconnect advances sequence", Nat.show(U.Ui.seq(delta)), "3") + C.Check.str("cache bounded to two hubs", Nat.show(List.length(&2, U.HubView, many)), "2") + C.Check.str("cache always retains home", Bool.show(Maybe.is_some(&2, U.HubView, U.HubCache.get(many, ""))), "True") + C.Check.str("cache evicts earlier linked hub", Bool.show(Maybe.is_some(&2, U.HubView, U.HubCache.get(many, remote()))), "False") + C.Check.str("wrong cache cannot install", Nat.show(U.Ui.seq(U.Ui.cached(Some{snapshot("wrong", "wrong")}, sw))), "0") + C.Check.str("missing bot stays pending on join", U.Ui.scratch(waiting, "@bot.want"), "Kit") + C.Check.str("later BotSet resolves bound want", U.Ui.sel(later), "remote-thread") + C.Check.str("thread request waits before log", U.Ui.sel(U.Ui.recv(thread, update())), "") + C.Check.str("manual choice cancels pending thread", U.Ui.scratch(U.Act.ui(U.Ui.act(thread, "select", "manual")), "@thread.want"), "") + C.Check.str("thread request resolves destination", U.Ui.sel(U.Ui.recv(thread, remote_log())), "remote-thread") + C.Check.str("reselect restores destination viewer", Bool.show(U.Ui.panel(with_view)), "True") + C.Check.str("chat has timeline", Bool.show(U.Ui.timeline(joined)), "True") + C.Check.str("space skips timeline", Bool.show(U.Ui.timeline(U.Ui.scratch_set(joined, "@btab", "space"))), "False") + C.Check.str("browser skips timeline", Bool.show(U.Ui.timeline(U.Ui.scratch_set(joined, "@btab", "browser"))), "False") + C.Check.str("settings skips timeline", Bool.show(U.Ui.timeline(U.Ui.flag_on(joined, "settings"))), "False") + C.Check.str("transition has no timeline", Bool.show(U.Ui.timeline(sw)), "False") diff --git a/test/hubs_test.bend b/test/hubs_test.bend index 1505de041c..8fae153ea5 100644 --- a/test/hubs_test.bend +++ b/test/hubs_test.bend @@ -83,7 +83,7 @@ def main() -> IO(Unit): C.Check.str("offline is a change", yes(A.online_same(two(), "box:3787", False{})), "no") C.Check.str("offline again is the same", yes(A.online_same(H.Hubs.after(H.Hubs.online(two(), "box:3787", False{})), "box:3787", False{})), "yes") C.Check.str("projects of both", list(two(), "projects", "id"), "100.1.1.1:3787|p1;box:3787|p2;") - C.Check.str("one list: machines unnamed while names differ", list(two(), "projects", "machine"), ";;") + C.Check.str("one list: other hub keeps its machine marker", list(two(), "projects", "machine"), ";box;") C.Check.str("thread ids qualified", rows(two()), "100.1.1.1:3787|t1;") C.Check.str("hubs listed", list(two(), "hubs", "name"), "lap;box;") C.Check.str("nothing selected", screen(two(), "sel"), "") @@ -91,7 +91,13 @@ def main() -> IO(Unit): C.Check.str("select moves to its hub", H.Hubs.cur(H.Hubs.after(H.Hubs.act(two(), "select", "box:3787|t2"))), "box:3787") C.Check.str("selected there", sel(H.Hubs.after(H.Hubs.act(two(), "select", "box:3787|t2")), "box:3787"), "t2") C.Check.str("screen sel qualified", screen(H.Hubs.after(H.Hubs.act(two(), "select", "box:3787|t2")), "sel"), "box:3787|t2") - C.Check.str("thread shown", J.Json.get_str(J.Json.get_obj(H.Hubs.screen(H.Hubs.after(H.Hubs.act(two(), "select", "box:3787|t2"))), "thread"), "title", ""), "Two") + C.Check.str("thread shown with its machine", J.Json.get_str(J.Json.get_obj(H.Hubs.screen(H.Hubs.after(H.Hubs.act(two(), "select", "box:3787|t2"))), "thread"), "title", ""), "Two · box") + C.Check.str("offline selected owner stays named", J.Json.get_str(J.Json.get_obj(H.Hubs.screen(H.Hubs.after(H.Hubs.online(H.Hubs.after(H.Hubs.act(two(), "select", "box:3787|t2")), "box:3787", False{}))), "thread"), "title", ""), "Two · box (away)") + C.Check.str("single hub title stays plain", J.Json.get_str(H.Hubs.origin.row(False{}, J.Json.obj([J.KV{"title", J.Str{"One"}}]), "lap", False{}), "title", ""), "One") + C.Check.str("active direct owner named", J.Json.get_str(H.Hubs.origin.row(True{}, J.Json.obj([J.KV{"id", J.Str{"box:3787|t2"}}, J.KV{"title", J.Str{"Two"}}]), "box", True{}), "title", ""), "Two · box") + C.Check.str("active owner id stays routable", J.Json.get_str(H.Hubs.origin.row(True{}, J.Json.obj([J.KV{"id", J.Str{"box:3787|t2"}}, J.KV{"title", J.Str{"Two"}}]), "box", False{}), "id", ""), "box:3787|t2") + C.Check.str("mirror title never names its relay", J.Json.get_str(H.Hubs.origin.row(True{}, J.Json.obj([J.KV{"id", J.Str{"lap:3787|p1~t2"}}, J.KV{"title", J.Str{"Two · box"}}]), "lap", True{}), "title", ""), "Two · box") + C.Check.str("reported-only title stays with owner", J.Json.get_str(H.Hubs.origin.row(True{}, J.Json.obj([J.KV{"kind", J.Str{"remote"}}, J.KV{"title", J.Str{"Kit · box"}}]), "lap", True{}), "title", ""), "Kit · box") C.Check.str("the hub left drops its thread", sel(H.Hubs.after(H.Hubs.act(H.Hubs.after(H.Hubs.act(two(), "select", "100.1.1.1:3787|t1")), "select", "box:3787|t2")), "100.1.1.1:3787"), "") C.Check.str("a thread's action goes to the current hub", diff --git a/test/machines_test.bend b/test/machines_test.bend index 0f84ec963e..9aa7a07f37 100644 --- a/test/machines_test.bend +++ b/test/machines_test.bend @@ -100,8 +100,11 @@ def main() -> IO(Unit): C.Check.str("remote log keeps the home list", rows(U.Machines.rows(U.Ui.recv(after(U.Ui.act(home(), "machine", "http://100.2.2.2:3787")), msg("{\"t\":\"log\",\"items\":[],\"info\":{\"machines\":\"{}\"}}")))), "laptop=|desktop=http://100.2.2.2:3787*|") - C.Check.str("a bot on the owner's machine opens there", cmds(U.Ui.act(linked(""), "remote", "Kit@p1")), "connect http://100.2.2.2:3787") - C.Check.str("and is shown once that hub's log comes", shown(U.Ui.recv(after(U.Ui.act(linked(""), "remote", "Kit@p1")), desk())), "b1/bt1/") + # the window no longer switches hubs to show a bot (that fetched the + # other hub's whole log): a bot not mirrored here is the conversation + # (a mirrored one opens as a bot, test/far_test.bend, farui_test.bend) + C.Check.str("a bot on the owner's machine never switches hubs", cmds(U.Ui.act(linked(""), "remote", "Kit@p1")), "none") + C.Check.str("and is the conversation until it is mirrored", shown(after(U.Ui.act(linked(""), "remote", "Kit@p1"))), "//Kit@p1") C.Check.str("a bot on someone else's machine is a conversation", cmds(U.Ui.act(linked(""), "remote", "Pal@p2")), "none") C.Check.str("shown as one", shown(after(U.Ui.act(linked(""), "remote", "Pal@p2"))), "//Pal@p2") C.Check.str("a machine name alone, off the tailnet, is not enough", cmds(U.Ui.act(U.Ui.info_set(U.Ui.info_set(home(), "bots.remote", fars()), "local.fixed", ""), "remote", "Kit@desktop")), "none") diff --git a/test/mbots_test.bend b/test/mbots_test.bend index 1a4f520c5b..7c2d9b3138 100644 --- a/test/mbots_test.bend +++ b/test/mbots_test.bend @@ -107,9 +107,10 @@ def main() -> IO(Unit): C.Check.str("no bot shown", J.Json.show(bot(two())), "null") C.Check.str("a bot moves the focus", H.Hubs.cur(shown()), "box:3787") C.Check.str("the bot shown", J.Json.get_str(bot(shown()), "name", ""), "Kit") + C.Check.str("the bot header keeps its owning machine", J.Json.get_str(bot(shown()), "title", ""), "Kit · box") C.Check.str("its id stays its hub's", J.Json.get_str(bot(shown()), "id", ""), "b1") C.Check.str("chat first, as the thread", J.Json.get_str(bot(shown()), "tab", ""), "chat") - C.Check.str("its thread is the chat", J.Json.get_str(J.Json.get_obj(H.Hubs.screen(shown()), "thread"), "title", ""), "Kit") + C.Check.str("its thread is the chat, with its owning machine", J.Json.get_str(J.Json.get_obj(H.Hubs.screen(shown()), "thread"), "title", ""), "Kit · box") C.Check.str("tabs", strs(J.Json.elems(J.Json.get_obj(bot(shown()), "tabs")), "label"), "Chat;Space;Browser;Memory;Routines;Webhooks;Settings;") C.Check.str("no tab content on chat", J.Json.show(J.Json.get_obj(bot(shown()), "memory")), "null") C.Check.str("memory", strs(J.Json.elems(J.Json.get_obj(tab("memory"), "memory")), "text"), "green;") diff --git a/test/native/far_bench.bend b/test/native/far_bench.bend new file mode 100644 index 0000000000..dc80845f18 --- /dev/null +++ b/test/native/far_bench.bend @@ -0,0 +1,189 @@ +import Base +import ../../src/server/server.bend as S +import ../../src/core/model.bend as M +import ../../src/core/client.bend as U +import ../../src/core/json.bend as J +import ../../src/core/store.bend as ST +import ../../src/app/nui.bend as N +import ../../src/app/layout.bend as L +import ../../src/app/theme.bend as T +import ../../src/gfx/raster.bend as R +import ../../src/gfx/tiles.bend as TI +import ../../src/app/win.bend as W +import ../../src/core/far.bend as FR +import ../../src/core/bot.bend as BT +import ../../src/app/bots.bend as BV +import ../../src/app/cats.bend as CA + +# Choosing a bot that lives on another machine, timed on a real event log: +# the log's own bots and their threads are made a linked machine's mirror +# (far.bend: its feed, as the owning hub would serve it, folded in under +# link p1), then the row the linked machine's directory reports is chosen +# ("remote", "name@box") and the frame after it drawn. Before, the window +# switched to that machine's hub and fetched its whole log +# (test/native/link_bench.bend: 11-20 s on a 13 MB log). +# scripts/build-app.sh test/native/far_bench.bend build/far_bench +# build/far_bench ~/.backplane-bend/events.jsonl (only reads it) + +def ops(o: L.Out) -> List<&2, R.Op>: + match o: + case L.Out{os, _}: + os + +def prepared(+n: N.Nui, +ms: Nat) -> N.Nui: + N.Lbx.stamp(N.Blink.stamp(N.Hover.stamp(N.Nui.prepare(n), ms), ms), ms) + +def seen(+n: Nat, +what: String, +a: Nat, +b: Nat) -> IO(Unit): + IO.print(what ++ ": " ++ Nat.show(Nat.sub(b, a)) ++ " ms (" ++ Nat.show(n) ++ ")") + +def count(s: M.State) -> Nat: + match s: + case M.State{_, _, es, _, _, _, _, _, _, _, _}: + List.length(&2, M.Entry, es) + +def some(f: TI.Tiles) -> Nat: + match f: + case _: + 1n + +def path(xs: List) -> String: + match xs: + case Nil{}: + "/dev/null" + case p <> Nil{}: + p + case _ <> t: + path(t) + +def ent_thread(e: M.Entry) -> String: + match e: + case M.Msg{_, t, _, _, _}: + t + case M.Act{_, t, _, _, _, _}: + t + +def entries(s: M.State) -> List<&2, M.Entry>: + match s: + case M.State{_, _, es, _, _, _, _, _, _, _, _}: + es + +# the thread of the first entry not in `not` +def other(es: List<&2, M.Entry>, +not: String) -> String: + match es: + case Nil{}: + "" + case +e <> t: + Bool.pick(String, String.eq(ent_thread(e), not), other(t, not), ent_thread(e)) + +def act_ui(a: U.Act) -> U.Ui: + match a: + case U.Act{u, _}: + u + +def hits(o: L.Out) -> List<&2, N.Hit>: + match o: + case L.Out{_, hs}: + hs + +def with_hits(n: N.Nui, hs: List<&2, N.Hit>, +ms: Nat) -> N.Nui: + match n: + case N.Nui{u, w, h, mx, my, sd, sc, ca, fo, pa, _, _, vw}: + N.Nui{u, w, h, mx, my, sd, sc, ca, fo, pa, hs, ms, vw} + +def step_nui(s: N.Step) -> N.Nui: + match s: + case N.Step{n, _, _}: + n + +# what one frame leaves for the next +type Kept is Data: + Kept{n: N.Nui, memos: List<&2, L.TLMemo>, frames: TI.Frames} + +def kept_n(k: Kept) -> N.Nui: + match k: + case Kept{n, _, _}: + n + +# Use the real bot window and skip wrapping for a hidden timeline. +def memos(visible: Bool, +ty: L.Ty, +n: N.Nui, old: List<&2, L.TLMemo>) -> List<&2, L.TLMemo>: + match visible: + case True{}: + L.Lay.memos(ty, n, old) + case False{}: + old + +def frame(+ty: L.Ty, +mm: List<&2, L.TLMemo>, +n: N.Nui) -> L.Out: + BV.Bv.frame(ty, L.TL.memos.now(mm), BV.Bv.warm(N.Nui.ui(n), CA.Cats.none(), 0n), n) + +def warm(+ty: L.Ty, +n0: N.Nui) -> Kept: + +n = prepared(n0, 0n) + +mm = memos(U.Ui.timeline(N.Nui.ui(n)), ty, n, Nil{}) + +o = frame(ty, mm, n) + Kept{with_hits(n, hits(o), 0n), mm, TI.FSome{N.Nui.width(n), N.Nui.height(n), TI.Frame.par(N.Nui.width(n), N.Nui.height(n), ops(o), Pix{T.Th.bg()}, TI.FNone{}, 0)}} + +# one event and the frame after it, timed: event, memo, layout, tiles (ms) +def step(+what: String, e: W.WinEv, +ty: L.Ty, k: Kept) -> IO(Kept): + match k: + case Kept{n0, memo0, fr0}: + do IO: + +t0 : Nat <- IO.now() + +n1 : N.Nui = prepared(step_nui(N.Nui.event(n0, e)), 7n) + +t1 : Nat <- IO.now() + +mm : List<&2, L.TLMemo> = memos(U.Ui.timeline(N.Nui.ui(n1)), ty, n1, memo0) + +t2 : Nat <- IO.now() + +o : L.Out = frame(ty, mm, n1) + +k2 : Nat = List.length(&2, R.Op, ops(o)) + +t3 : Nat <- IO.now() + +tiles : TI.Tiles = TI.Frame.par(N.Nui.width(n1), N.Nui.height(n1), ops(o), Pix{T.Th.bg()}, fr0, 0) + +img : Image = TI.Tiles.image(tiles) + +t4 : Nat <- IO.now() + IO.print(what ++ ": event " ++ Nat.show(Nat.sub(t1, t0)) ++ " memo " ++ Nat.show(Nat.sub(t2, t1)) ++ " layout " ++ Nat.show(Nat.sub(t3, t2)) + ++ " tiles " ++ Nat.show(Nat.sub(t4, t3)) ++ " = " ++ Nat.show(Nat.sub(t4, t0)) ++ " ms (" ++ Nat.show(k2) ++ " ops)") + return Kept{with_hits(n1, hits(o), t4), mm, TI.FSome{N.Nui.width(n1), N.Nui.height(n1), tiles}} + + +def first_far(bs: List<&2, BT.Bot>) -> String: + match bs: + case Nil{}: + "" + case +b <> t: + Bool.pick(String, FR.Fr.is(BT.Bot.id(b)), BT.Bot.name(b), first_far(t)) + +def main() -> IO(Unit): + do IO: + args : List <- IO.args() + +file : String = path(args) + +bs : List<&2, U32> <- S.Files.read(file) + +ld : ST.Loaded = ST.Store.load(S.Store.lines(bs), ST.Loaded{M.State.empty(), Nil{}}) + +st : M.State = ST.Loaded.st(ld) + +ls : List<&2, String> = ST.Loaded.wire(ld) + +u0 : U.Ui = U.Ui.adopt(U.Ui.recv(U.Ui.init(1790000000n), Maybe.default(&2, J.Json, J.Json.parse("{\"t\":\"log\",\"since\":0,\"items\":[]}"), J.Null{})), M.State.private(st)) + +t0 : Nat <- IO.now() + +feed : List<&2, FR.Line> = FR.Fr.feed(ls, st) + +t1 : Nat <- IO.now() + seen(List.length(&2, FR.Line, feed), "owning hub: its feed from the log (shared lines)", t0, t1) + +msg : String = FR.Fr.msg("p1", "box", False{}, 0n, List.length(&2, String, ls), FR.Fr.lines.items(feed, Nil{})) + +u1 : U.Ui = U.Ui.recv(U.Ui.recv(u0, Maybe.default(&2, J.Json, J.Json.parse(msg), J.Null{})), + Maybe.default(&2, J.Json, J.Json.parse("{\"t\":\"log\",\"since\":1,\"items\":[]}"), J.Null{})) + +t2 : Nat <- IO.now() + seen(String.length(msg), "client: the mirror folded in (chars)", t1, t2) + +name : String = first_far(BT.World.bots(M.State.bots(U.Ui.state(u1)))) + +info : String = "{\"t\":\"info\",\"info\":{\"bots.remote\":" ++ J.Json.show(J.Str{"[{\"name\":\"" ++ name ++ "\",\"peer\":\"p1\",\"peerName\":\"box\",\"look\":1,\"state\":\"ready\"},{\"name\":\"visitor\",\"peer\":\"p1\",\"peerName\":\"box\",\"look\":1,\"state\":\"ready\"}]"}) ++ "}}" + +u2 : U.Ui = U.Ui.recv(u1, Maybe.default(&2, J.Json, J.Json.parse(info), J.Null{})) + +ty : L.Ty <- L.Ty.load() + +k : Kept = warm(ty, N.Nui.init(u2, 1400, 900)) + IO.print("choosing " ++ name ++ "@box:") + +n0 : N.Nui = kept_n(k) + +t3 : Nat <- IO.now() + +u3 : U.Ui = act_ui(U.Ui.act(N.Nui.ui(n0), "remote", name ++ "@box")) + +t4 : Nat <- IO.now() + seen(0n, " the choice (state)", t3, t4) + IO.print(" selected " ++ U.Ui.scratch(u3, "@bot") ++ " thread " ++ U.Ui.sel(u3) ++ ", entries " ++ Nat.show(List.length(&2, M.Entry, M.State.mine(U.Ui.state(u3), U.Ui.sel(u3))))) + k2 : Kept <- step(" its first frame", W.WExpose{}, ty, Kept{N.Nui.with_ui(n0, u3), Nil{}, TI.FNone{}}) + k3 : Kept <- step(" the next frame", W.WExpose{}, ty, k2) + +t5 : Nat <- IO.now() + seen(0n, "total, choice to second frame", t3, t5) + +local : String = BT.Bot.id(Maybe.default(&2, BT.Bot, List.head(&2, BT.Bot, BT.World.bots(M.State.bots(st))), BT.Bot.blank(""))) + k4 : Kept <- step("local bot first frame", W.WExpose{}, ty, Kept{N.Nui.with_ui(n0, act_ui(U.Ui.act(u2, "bot", local))), Nil{}, TI.FNone{}}) + k5 : Kept <- step("reported-only bot first frame", W.WExpose{}, ty, Kept{N.Nui.with_ui(n0, act_ui(U.Ui.act(u2, "remote", "visitor@box"))), Nil{}, TI.FNone{}}) + return Unit{} diff --git a/test/native/hub_link_test.bend b/test/native/hub_link_test.bend new file mode 100644 index 0000000000..957c55c490 --- /dev/null +++ b/test/native/hub_link_test.bend @@ -0,0 +1,99 @@ +import Base +import ../check.bend as C +import ../../src/app/link.bend as L +import ../../src/server/server.bend as S +import ../../src/server/host.bend as H +import ../../src/core/json.bend as J +import ../../src/core/utf8.bend as U + +# Native network regression probe (temporary hubs only): +# scripts/build-app.sh test/native/hub_link_test.bend build/hub_link_test +# build/hub_link_test http://localhost:3792 [SINCE ORIGIN] +# build/hub_link_test http://localhost:3794 (a silent HTTP peer) +# PATH=:$PATH ... whois +# The link must yield a log or an explicit failure, never wait indefinitely. +# The whois probe supplies no pairing token: partial stdout from a timed-out +# command must never authenticate it, even when it names the right owner. + +def Probe.object(text: String) -> J.Json: + Maybe.default(&2, J.Json, J.Json.parse(text), J.Null{}) + +def Probe.done(+o: J.Json) -> Bool: + Bool.or(String.eq(J.Json.get_str(o, "t", ""), "log"), Bool.not(String.is_empty(J.Json.get_str(o, "error", "")))) + +def Probe.stop(done: Bool, text: String, k: Unit -> IO(String)) -> IO(String): + match done: + case True{}: IO.pure(String, text) + case False{}: k(Unit{}) + +def Probe.wire(m: Maybe<&1, S.Wire>, k: Unit -> IO(String)) -> IO(String): + match m: + case Some{S.WText{+text}}: + Probe.stop(Probe.done(Probe.object(text)), text, k) + case Some{_}: k(Unit{}) + case None{}: IO.pure(String, "closed without a result") + +def Probe.read(fuel: Nat, +out: Chan(S.Wire)) -> IO(String): + match fuel: + case 0n: IO.pure(String, "too many messages before log") + case 1n+k: + IO.bind(Maybe<&1, S.Wire>, String, Chan.recv(S.Wire, out), m => Probe.wire(m, u => Probe.read(k, out))) + +def Probe.link(url: String, since: Nat, origin: String) -> IO(Unit): + do IO: + +inbox : Chan(S.Msg) <- L.Link.start(url) + +out : Chan(S.Wire) <- Chan.new(S.Wire, 8192) + +t0 : Nat <- IO.now() + ok : Bool <- Chan.send(S.Msg, inbox, S.Join{0, out, since, origin}) + +text : String <- Probe.read(10n, out) + +t1 : Nat <- IO.now() + IO.print("link elapsed ms=" ++ Nat.show(Nat.sub(t1, t0)) ++ " " ++ text) + C.Check.str("link returns log or explicit unreachable", Bool.show(Probe.done(Probe.object(text))), "True") + C.Check.str("connect and handshake are bounded", Bool.show(Nat.is_le(Nat.sub(t1, t0), 11000n)), "True") + left : Bool <- Chan.send(S.Msg, inbox, S.Leave{0}) + return Unit{} + +def Probe.accepted(r: Listener & Result<&1, &1, U32 & String, Socket>) -> IO(Unit): + (listener, result) = r + match result: + case Done{s}: + do IO: + Listener.close(listener) + +inbox : Chan(S.Msg) <- Chan.new(S.Msg, 16) + S.Accept.whois((s, "127.0.0.1"), "owner@test", 7, "", inbox, "pairing-token") + case Fail{_}: + Listener.close(listener) + +def Probe.response(r: Socket & Result<&1, &1, U32 & String, List<&2, U32>>, +t0: Nat) -> IO(Unit): + (s, response) = r + do IO: + Socket.close(s) + +t1 : Nat <- IO.now() + +text : String = U.Utf8.decode(Result.default(&1, &1, U32 & String, List<&2, U32>, response, Nil{})) + C.Check.str("timed out identity cannot authenticate", Bool.show(String.starts_with(text, "HTTP/1.1 401 Unauthorized")), "True") + C.Check.str("whois timeout exercised", Bool.show(Nat.is_ge(Nat.sub(t1, t0), 4500n)), "True") + C.Check.str("whois wait is bounded", Bool.show(Nat.is_le(Nat.sub(t1, t0), 6500n)), "True") + IO.print("whois elapsed ms=" ++ Nat.show(Nat.sub(t1, t0)) ++ " " ++ text) + +def Probe.sent(r: Socket & Result<&1, &1, U32 & String, Unit>, t0: Nat) -> IO(Unit): + (s, _) = r + IO.bind(Socket & Result<&1, &1, U32 & String, List<&2, U32>>, Unit, H.Sock.recv_bytes(s, 65536), r2 => Probe.response(r2, t0)) + +def Probe.whois() -> IO(Unit): + do IO: + listener : Listener <- IO.try(Listener, TCP.listen(3891)) + IO.spawn(Unit, IO.bind(Listener & Result<&1, &1, U32 & String, Socket>, Unit, TCP.accept(listener), Probe.accepted)) + s : Socket <- IO.try(Socket, TCP.connect("127.0.0.1", 3891)) + t0 : Nat <- IO.now() + IO.bind(Socket & Result<&1, &1, U32 & String, Unit>, Unit, H.Sock.send_text(s, "GET /ws HTTP/1.1\r\nHost: localhost\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n"), r => Probe.sent(r, t0)) + +def Probe.args(xs: List<&1, String>) -> IO(Unit): + match xs: + case "whois" <> _: Probe.whois() + case url <> count <> origin <> _: + Probe.link(url, Maybe.default(&2, Nat, Nat.read(count), 0n), origin) + case url <> _: Probe.link(url, 0n, "") + case Nil{}: IO.print("pass a temporary hub URL or whois") + +def main() -> IO(Unit): + IO.bind(List<&1, String>, Unit, IO.args(), Probe.args) diff --git a/test/native/link_bench.bend b/test/native/link_bench.bend new file mode 100644 index 0000000000..cb8d31131f --- /dev/null +++ b/test/native/link_bench.bend @@ -0,0 +1,90 @@ +import Base +import ../../src/server/server.bend as S +import ../../src/core/model.bend as M +import ../../src/core/client.bend as U +import ../../src/core/json.bend as J +import ../../src/app/link.bend as LK + +# What switching the window to another machine's hub costs: a link +# (src/app/link.bend) joined as the window joins it, timed until the hub's +# log arrives, then the window's parse and replay of it. +# scripts/build-app.sh test/native/link_bench.bend build/link_bench +# build/link_bench http://127.0.0.1:3790 + +def seen(+n: Nat, +what: String, +a: Nat, +b: Nat) -> IO(Unit): + IO.print(what ++ ": " ++ Nat.show(Nat.sub(b, a)) ++ " ms (" ++ Nat.show(n) ++ ")") + +def count(s: M.State) -> Nat: + match s: + case M.State{_, _, es, _, _, _, _, _, _, _, _}: + List.length(&2, M.Entry, es) + +def url(xs: List) -> String: + match xs: + case Nil{}: + "http://127.0.0.1:3787" + case p <> Nil{}: + p + case _ <> t: + url(t) + +def is_log(+t: String) -> Bool: + String.starts_with(t, "{\"t\":\"log\"") + +def got(m: Maybe<&1, S.Wire>, k: Maybe<&2, String> -> IO(Maybe<&2, String>), +t0: Nat) -> IO(Maybe<&2, String>): + match m: + case Some{S.WText{+t}}: + do IO>: + +t1 : Nat <- IO.now() + IO.print(" +" ++ Nat.show(Nat.sub(t1, t0)) ++ " ms: " ++ String.take(t, 60n) ++ " (" ++ Nat.show(String.length(t)) ++ " chars)") + k(Bool.pick(Maybe<&2, String>, is_log(t), Some{t}, None{})) + case Some{_}: + k(None{}) + case None{}: + IO.pure(Maybe<&2, String>, None{}) + +def found(m: Maybe<&2, String>, loop: Unit -> IO(Maybe<&2, String>)) -> IO(Maybe<&2, String>): + match m: + case Some{t}: + IO.pure(Maybe<&2, String>, Some{t}) + case None{}: + loop(Unit{}) + +# the messages until the log +def wait(fuel: Nat, +out: Chan(S.Wire), +t0: Nat) -> IO(Maybe<&2, String>): + match fuel: + case 0n: + IO.pure(Maybe<&2, String>, None{}) + case 1n+f: + IO.bind(Maybe<&1, S.Wire>, Maybe<&2, String>, Chan.recv(S.Wire, out), + m => got(m, r => found(r, u => wait(f, out, t0)), t0)) + +def replay(m: Maybe<&2, String>, +t1: Nat) -> IO(Unit): + match m: + case None{}: + IO.print("no log") + case Some{+text}: + do IO: + +o : J.Json = Maybe.default(&2, J.Json, J.Json.parse(text), J.Null{}) + +t2 : Nat <- IO.now() + seen(List.length(&2, J.Json, J.Json.elems(J.Json.get_obj(o, "items"))), "window: parse log", t1, t2) + +u : U.Ui = U.Ui.recv(U.Ui.init(1790000000n), o) + +t3 : Nat <- IO.now() + seen(count(U.Ui.state(u)), "window: replay", t2, t3) + +def main() -> IO(Unit): + do IO: + args : List <- IO.args() + +to : String = url(args) + +t0 : Nat <- IO.now() + +inbox : Chan(S.Msg) <- LK.Link.start(to) + +out : Chan(S.Wire) <- Chan.new(S.Wire, 8192) + ok : Bool <- Chan.send(S.Msg, inbox, S.Join{0, out, 0n, ""}) + +m : Maybe<&2, String> <- wait(64n, out, t0) + +t1 : Nat <- IO.now() + seen(0n, "link: connect, fetch and decode the log", t0, t1) + replay(m, t1) + +t4 : Nat <- IO.now() + seen(0n, "total", t0, t4) + ok2 : Bool <- Chan.send(S.Msg, inbox, S.Leave{0}) + return Unit{} diff --git a/test/native/wrap_bench.bend b/test/native/wrap_bench.bend new file mode 100644 index 0000000000..d259d14d1f --- /dev/null +++ b/test/native/wrap_bench.bend @@ -0,0 +1,112 @@ +import Base +import ../../src/gfx/text.bend as X +import ../../src/app/bots.bend as BV +import ../../src/app/layout.bend as L +import ../../src/core/bot.bend as BT + +# Each invocation measures one fresh process, with ASCII warmed as in the +# window. Construction/font loading are outside the timer; all lines are +# consumed inside it. Width is the investigation's 780 px. +# scripts/build-app.sh test/native/wrap_bench.bend build/wrap_bench +# build/wrap_bench [10000|90000] [token|normal|cache|current] + +def word(n: Nat, acc: String) -> String: + match n: + case 0n: + acc + case 1n+k: + word(k, SCon{Chr{120}, acc}) + +def normal(n: Nat, acc: String) -> String: + match n: + case 0n: + acc + case 1n+k: + normal(k, "Check the board and its power rails before routing. " ++ acc) + +def sample(token: Bool, +n: Nat) -> String: + match token: + case True{}: + word(n, "") + case False{}: + String.take(normal(1n+Nat.div(n, 50n), ""), n) + +def chars(ls: List<&2, String>, acc: Nat) -> Nat: + match ls: + case Nil{}: + acc + case l <> t: + chars(t, Nat.add(acc, String.length(l))) + +def arg(xs: List) -> String & String: + match xs: + case a <> b <> _: + (a, b) + case a <> _: + (a, "token") + case Nil{}: + ("10000", "token") + +# "current" replays the previous one-conversation policy, with the new +# linear fitter, to isolate the benefit of retaining conversation groups. +# "cache" uses the actual bounded cache. Both run A -> B -> A in a fresh +# process and consume every wrapped character before stopping the timer. +def Cache.update(retain: Bool, +ps: List<&2, BT.Post>, +room: String, ws: Map<&2, BV.PW>, +ty: L.Ty) -> Map<&2, BV.PW>: + match retain: + case True{}: + BV.Bv.wraps.conversation(ps, room, ws, ty, 780) + case False{}: + BV.Bv.wraps.put(ps, ws, ty, 780, BV.Bv.wraps.none()) + +def Cache.posts(retain: Bool, ws: Map<&2, BV.PW>, +room: String) -> Map<&2, BV.PW>: + match retain: + case True{}: + BV.Bv.wraps.group(ws, room, 780) + case False{}: + ws + +def Cache.step(+retain: Bool, +ty: L.Ty, +room: String, +text: String, ws: Map<&2, BV.PW>, name: String) -> IO(Map<&2, BV.PW>): + do IO>: + +t0 : Nat <- IO.now() + +next : Map<&2, BV.PW> = Cache.update(retain, [BT.Post{room, room, "you", text, 0n, 0n}], room, ws, ty) + +ls : List<&2, String> = BV.Bv.wrap(Cache.posts(retain, next, room), ty, room, text, 780) + +lines : Nat = List.length(&2, String, ls) + +kept : Nat = chars(ls, 0n) + +t1 : Nat <- IO.now() + IO.print(name ++ " wrap_ms=" ++ Nat.show(Nat.sub(t1, t0)) ++ " lines=" ++ Nat.show(lines) ++ " kept=" ++ Nat.show(kept)) + return next + +def Cache.run(+retain: Bool, +f: X.Face, c: Map<&2, X.TG>, +n: Nat) -> IO(Unit): + do IO: + +ty : L.Ty = L.Ty{c, f, f, f, f, f} + +text : String = word(n, "") + a : Map<&2, BV.PW> <- Cache.step(retain, ty, "a", text, BV.Bv.wraps.none(), "first A") + b : Map<&2, BV.PW> <- Cache.step(retain, ty, "b", "A short other conversation", a, "switch B") + back : Map<&2, BV.PW> <- Cache.step(retain, ty, "a", text, b, "return A") + IO.print("groups=" ++ Nat.show(List.length(&2, String, BV.Bv.wraps.keys(back)))) + +def measure(cache: Bool, +kind: String, +n: Nat, +s: String, +f: X.Face, +c: Map<&2, X.TG>) -> IO(Unit): + match cache: + case True{}: + Cache.run(String.eq(kind, "cache"), f, c, n) + case False{}: + do IO: + +t0 : Nat <- IO.now() + +ls : List<&2, String> = X.Text.wrap(c, f, s, 780) + +lines : Nat = List.length(&2, String, ls) + +kept : Nat = chars(ls, 0n) + +t1 : Nat <- IO.now() + IO.print(kind ++ " chars=" ++ Nat.show(n) ++ " wrap_ms=" ++ Nat.show(Nat.sub(t1, t0)) ++ " lines=" ++ Nat.show(lines) ++ " kept=" ++ Nat.show(kept) ++ " face=" ++ X.Face.name(f)) + +def run(a: String & String) -> IO(Unit): + (+size, +kind) = a + do IO: + +n : Nat = Maybe.default(&2, Nat, Nat.read(size), 10000n) + +s : String = sample(String.eq(kind, "token"), n) + +f : X.Face <- X.Text.face(["Berkeley Mono", "monospace"], 14) + +c : Map<&2, X.TG> <- X.Text.warm(Map.new(&2, X.TG), f) + +ft : X.Face = X.Face.tabled(f, c) + measure(Bool.or(String.eq(kind, "cache"), String.eq(kind, "current")), kind, n, s, ft, c) + +def main() -> IO(Unit): + IO.bind(List, Unit, IO.args(), as => run(arg(as))) diff --git a/test/text_wrap_test.bend b/test/text_wrap_test.bend new file mode 100644 index 0000000000..9351a4f354 --- /dev/null +++ b/test/text_wrap_test.bend @@ -0,0 +1,192 @@ +import Base +import ./check.bend as C +import ../src/gfx/text.bend as X +import ../src/app/bots.bend as BV +import ../src/app/layout.bend as L +import ../src/core/bot.bend as BT +import ../src/core/botui.bend as BU +import ../src/core/client.bend as U +import ../src/core/edit.bend as E +import ../src/app/nui.bend as N + +# The previous eager implementation, retained only as an output oracle. +def Old.count(s: String, +cache: Map<&2, X.TG>, +f: X.Face, +max: U32, +used: U32) -> Nat: + match s: + case SNil{}: + 0n + case SCon{Chr{+cp}, t}: + +u2 = (used + X.TG.adv(X.Text.get(cache, f, cp)) : U32) + X.Fit.count.k(U32.is_le(u2, max), Old.count(t, cache, f, max, u2)) + +def Old.chunk(n: Nat, empty: Bool, +s: String, +cache: Map<&2, X.TG>, +f: X.Face, +max: U32) -> List<&2, String>: + match n empty: + case 1n+m False{}: + +k = Bool.pick(Nat, Nat.is_eq(Old.count(s, cache, f, max, 0), 0n), 1n, Old.count(s, cache, f, max, 0)) + String.take(s, k) <> Old.chunk(m, String.is_empty(String.drop(s, k)), String.drop(s, k), cache, f, max) + case _ _: + Nil{} + +def Old.chunks(empty: Bool, +w: String, +cache: Map<&2, X.TG>, +f: X.Face, +max: U32) -> List<&2, String>: + match empty: + case True{}: + [""] + case False{}: + Old.chunk(1n+String.length(w), False{}, w, cache, f, max) + +def Old.words(ws: List<&2, String>, a: X.PAcc, +cache: Map<&2, X.TG>, +f: X.Face, +sp: U32, +max: U32) -> X.PAcc: + match ws: + case Nil{}: + a + case +w <> t: + Old.words(t, X.PWrap.pieces(Old.chunks(String.is_empty(w), w, cache, f, max), a, cache, f, sp, max), cache, f, sp, max) + +def Old.line(s: String, +cache: Map<&2, X.TG>, +f: X.Face, +px: U32) -> List<&2, String>: + X.PWrap.fin(Old.words(String.split(s, ' '), X.PAcc{Nil{}, "", 0, True{}}, cache, f, X.TG.adv(X.Text.get(cache, f, 32)), (px * 64 : U32))) + +def Old.lines(ls: List<&2, String>, +cache: Map<&2, X.TG>, +f: X.Face, +px: U32) -> List<&2, String>: + match ls: + case Nil{}: + Nil{} + case l <> t: + List.append(&2, String, Old.line(l, cache, f, px), Old.lines(t, cache, f, px)) + +def Old.wrap(+cache: Map<&2, X.TG>, +f: X.Face, s: String, +px: U32) -> List<&2, String>: + Old.lines(String.split(s, '\n'), cache, f, px) + +# Length-prefixed lines compare exact boundaries, including empty lines. +def show(ls: List<&2, String>, acc: String) -> String: + match ls: + case Nil{}: + acc + case +l <> t: + show(t, acc ++ Nat.show(String.length(l)) ++ ":" ++ l ++ "|") + +def word(n: Nat, acc: String) -> String: + match n: + case 0n: + acc + case 1n+k: + word(k, SCon{Chr{120}, acc}) + +def compare(+cache: Map<&2, X.TG>, +f: X.Face, +s: String, +px: U32, name: String) -> IO(Unit): + C.Check.str(name, show(X.Text.wrap(cache, f, s, px), ""), show(Old.wrap(cache, f, s, px), "")) + +def samples(+cache: Map<&2, X.TG>, +f: X.Face, +px: U32) -> IO(Unit): + do IO: + compare(cache, f, "", px, "empty") + compare(cache, f, "plain words and a longer sentence", px, "normal") + compare(cache, f, " leading repeated spaces ", px, "spaces") + compare(cache, f, "\n\nline one\nline two\n", px, "newlines") + compare(cache, f, "日本語漢字かなカナ 日本語", px, "CJK") + compare(cache, f, "😀🚀🙂👩‍🔬 🏳️‍🌈 👍🏽", px, "emoji combining and ZWJ") + compare(cache, f, "aaaa bbb ccc xx a", px, "variable advances") + compare(cache, f, word(Bool.pick(Nat, U32.is_lt(px, 8), 128n, 2048n), ""), px, "long unbroken oracle") + +def widths(ws: List<&2, U32>, +cache: Map<&2, X.TG>, +f: X.Face) -> IO(Unit): + match ws: + case Nil{}: + IO.pure(Unit, Unit{}) + case w <> t: + IO.bind(Unit, Unit, samples(cache, f, w), u => widths(t, cache, f)) + +# Synthetic fractional advances, wider CJK and emoji, and zero-width +# joiners exercise cached metrics independently of the host's fonts. +def metrics() -> Map<&2, X.TG>: + Map.set(&2, X.TG, Map.set(&2, X.TG, Map.set(&2, X.TG, Map.set(&2, X.TG, + Map.set(&2, X.TG, Map.new(&2, X.TG), X.Text.key(0, 97), X.TG.blank(211)), + X.Text.key(0, 32), X.TG.blank(129)), X.Text.key(0, 26085), X.TG.blank(1024)), + X.Text.key(0, 128512), X.TG.blank(1280)), X.Text.key(0, 8205), X.TG.blank(0)) + +def ty(+f: X.Face) -> L.Ty: + L.Ty{Map.new(&2, X.TG), f, f, f, f, f} + +def post(+id: String, +text: String) -> List<&2, BT.Post>: + [BT.Post{id, "", "you", text, 0n, 1n}] + +def got(+ws: Map<&2, BV.PW>, +room: String, +w: U32, +id: String) -> BV.PW: + BV.Bv.wrap.got(Map.get(BV.PW, BV.PW{False{}, "", Nil{}}, BV.Bv.wraps.group(ws, room, w), BV.Bv.wrap.key(id, w))) + +def has(p: BV.PW) -> String: + match p: + case BV.PW{True{}, _, _}: + "yes" + case _: + "no" + +def seed(+id: String, +w: U32, text: String, lines: List<&2, String>) -> Map<&2, BV.PW>: + Map.set(&2, BV.PW, BV.Bv.wraps.none(), BV.Bv.wrap.key(id, w), BV.PW{True{}, text, lines}) + +def fill(+n: Nat, +ty: L.Ty, ws: Map<&2, BV.PW>) -> Map<&2, BV.PW>: + match n: + case 0n: + ws + case 1n+k: + fill(k, ty, BV.Bv.wraps.conversation(post("same-id", "hello"), Nat.show(k), ws, ty, 24)) + +def cache() -> IO(Unit): + do IO: + +t : L.Ty = ty(X.Text.bitmap()) + +a : Map<&2, BV.PW> = BV.Bv.wraps.keep(BV.Bv.wraps.none(), BV.Bv.wraps.key("a", 24), seed("p", 24, "hello", ["cached hit"])) + +b : Map<&2, BV.PW> = BV.Bv.wraps.conversation(post("p", "world"), "b", a, t, 24) + +back : Map<&2, BV.PW> = BV.Bv.wraps.conversation(post("p", "hello"), "a", b, t, 24) + C.Check.str("switch back uses cached lines", show(BV.Bv.wrap(BV.Bv.wraps.group(back, "a", 24), t, "p", "hello", 24), ""), "10:cached hit|") + C.Check.str("same post id in another conversation", show(BV.Bv.wrap(BV.Bv.wraps.group(back, "b", 24), t, "p", "world", 24), ""), "3:wor|2:ld|") + +wide : Map<&2, BV.PW> = BV.Bv.wraps.conversation(post("p", "hello"), "a", back, t, 80) + C.Check.str("width variant retained", has(got(wide, "a", 24, "p")), "yes") + C.Check.str("new width wraps separately", show(BV.Bv.wrap(BV.Bv.wraps.group(wide, "a", 80), t, "p", "hello", 80), ""), "5:hello|") + +changed : Map<&2, BV.PW> = BV.Bv.wraps.conversation(post("p", "changed"), "a", wide, t, 24) + C.Check.str("changed text invalidates", show(BV.Bv.wrap(BV.Bv.wraps.group(changed, "a", 24), t, "p", "changed", 24), ""), "3:cha|3:nge|1:d|") + +gone : Map<&2, BV.PW> = BV.Bv.wraps.conversation(Nil{}, "a", changed, t, 24) + C.Check.str("removed posts pruned", has(got(gone, "a", 24, "p")), "no") + +eight : Map<&2, BV.PW> = fill(8n, t, BV.Bv.wraps.none()) + +recent : Map<&2, BV.PW> = BV.Bv.wraps.conversation(post("same-id", "hello"), "7", eight, t, 24) + +nine : Map<&2, BV.PW> = BV.Bv.wraps.conversation(post("same-id", "hello"), "new", recent, t, 24) + C.Check.str("eviction frees group maps", Nat.show(List.length(&2, String, Map.keys(&2, BV.PW, nine))), "9") + C.Check.str("eight groups bounded", Nat.show(List.length(&2, String, BV.Bv.wraps.keys(nine))), "8") + C.Check.str("recent group retained", has(got(nine, "7", 24, "same-id")), "yes") + C.Check.str("least recent group evicted", has(got(nine, "6", 24, "same-id")), "no") + C.Check.str("ordinary thread preserves groups", Nat.show(List.length(&2, String, BV.Bv.wraps.keys(BV.Bv.wraps.of(BU.VThread{}, U.Ui.init(1n), nine, t, 24)))), "8") + C.Check.str("Settings preserves groups", Nat.show(List.length(&2, String, BV.Bv.wraps.keys(BV.Bv.wraps.k(True{}, t, N.Nui.init(U.Ui.init(1n), 1400, 900), nine)))), "8") + C.Check.str("explicit glyph reset clears groups", Nat.show(List.length(&2, String, BV.Bv.wraps.keys(BV.Bv.wraps.none()))), "0") + +# Count/check every code point without a recursive equality over one +# giant String (the default test runner has a much smaller stack). +def allx(s: String, +ok: Bool) -> Bool: + match s: + case SNil{}: + ok + case SCon{Chr{cp}, t}: + allx(t, Bool.and(ok, U32.is_eq(cp, 120))) + +def shape(ls: List<&2, String>, +ok: Bool) -> Bool: + match ls: + case Nil{}: + ok + case +l <> t: + shape(t, Bool.and(ok, Bool.and(allx(l, True{}), Nat.is_le(String.length(l), 97n)))) + +def total(ls: List<&2, String>, acc: Nat) -> Nat: + match ls: + case Nil{}: + acc + case l <> t: + total(t, Nat.add(acc, String.length(l))) + +# A size that made the old suffix walk quadratic. Check content and count +# without running that quadratic oracle at 9k in the test. +def long() -> IO(Unit): + do IO: + +s : String = word(9000n, "") + +ls : List<&2, String> = X.Text.wrap(Map.new(&2, X.TG), X.Text.bitmap(), s, 776) + C.Check.str("9k unbroken content and widths", Bool.pick(String, shape(ls, True{}), "yes", "no"), "yes") + C.Check.str("9k all characters kept", Nat.show(total(ls, 0n)), "9000") + C.Check.str("9k unbroken line count", Nat.show(List.length(&2, String, ls)), "93") + C.Check.str("9k last piece", Nat.show(String.length(Maybe.default(&2, String, List.last(&2, String, ls), ""))), "76") + C.Check.str("line starts consume suffix", Nat.show(Maybe.default(&2, Nat, List.last(&2, Nat, E.Edit.starts(ls, s)), 0n)), "8924") + +def main() -> IO(Unit): + do IO: + widths([0, 1, 7, 8, 16, 24, 97, 780], Map.new(&2, X.TG), X.Text.bitmap()) + widths([0, 8, 24, 97], metrics(), X.Text.bitmap()) + long() + cache() diff --git a/test/tools/botdir_e2e.ts b/test/tools/botdir_e2e.ts new file mode 100644 index 0000000000..c8f6533709 --- /dev/null +++ b/test/tools/botdir_e2e.ts @@ -0,0 +1,121 @@ +// Directory timing and persistence with two real temp-home hubs linked +// through bots.invite/bots.join, plus a TCP peer that can hang forever. +// bun test/tools/botdir_e2e.ts BINARY [WIREDIR] [--baseline] +import { createHmac } from "node:crypto"; +import { mkdtempSync, mkdirSync, readFileSync, readdirSync, writeFileSync, chmodSync, rmSync, existsSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +const [bin = "build/dir-after", wire = "build/wire"] = process.argv.slice(2).filter(x => !x.startsWith("--")); +const baseline = process.argv.includes("--baseline"); +for (const f of readdirSync(wire).filter(x => x.endsWith(".js"))) (0, eval)(readFileSync(join(wire, f), "utf8")); +const W = (globalThis as any).Wire; +const root = mkdtempSync(join(tmpdir(), "bp-botdir-")); +const fakebin = join(root, "bin"); mkdirSync(fakebin); +for (const n of ["claude", "codex", "grok"]) { writeFileSync(join(fakebin, n), "#!/bin/sh\nexit 1\n"); chmodSync(join(fakebin, n), 0o755); } +const sleep = (n: number) => new Promise(r => setTimeout(r, n)); +let failed = false; +function check(name: string, ok: unknown, got?: unknown) { console.log(`${ok ? "ok" : "FAIL"} ${name}${ok ? "" : ": " + JSON.stringify(got)}`); if (!ok) failed = true; } +async function until(ms: number, f: () => any) { const end = Date.now() + ms; do { const v = f(); if (v) return v; await sleep(20); } while (Date.now() < end); } +function freePort() { const s = Bun.listen({ hostname: "127.0.0.1", port: 0, socket: { data() {} } }); const p = s.port; s.stop(true); return p; } +const procs: ReturnType[] = []; +type Hub = { name: string; home: string; port: number; proc: ReturnType; ws: WebSocket; n: number; replies: Map; info: any; seen: any[]; started: number; infoAt: number; log: string }; +async function start(name: string, port = freePort()): Promise { + const home = join(root, name); const started = Date.now(); + const proc = Bun.spawn([resolve(bin), "--home", home, "--port", String(port), "--no-tailscale"], { + env: { ...process.env, HOME: root, DISPLAY: "", WAYLAND_DISPLAY: "", BACKPLANE_NAME: name, BACKPLANE_NO_UPDATE: "1", BACKPLANE_PEERS: "", PATH: `${fakebin}:${process.env.PATH}` }, stdout: "ignore", stderr: "pipe" }); + procs.push(proc); + const h: Hub = { name, home, port, proc, ws: null as any, n: 0, replies: new Map(), info: null, seen: [], started, infoAt: 0, log: "" }; + void (async () => { for await (const b of proc.stderr as any) h.log += new TextDecoder().decode(b); })(); + for (let i = 0; i < 500; i++) { try { if ((await fetch(`http://127.0.0.1:${port}/hello`)).ok) break; } catch {} await sleep(20); } + const ws = new WebSocket(`ws://127.0.0.1:${port}/ws`); ws.binaryType = "arraybuffer"; + ws.onmessage = e => { const o = JSON.parse(W.decode(new Uint8Array(e.data as ArrayBuffer))); if (o.info) { h.info = o.info; if (!h.infoAt) h.infoAt = Date.now(); } if (o.t === "reply") h.replies.set(Number(o.id), o); h.seen.push(...(o.items ?? [])); }; + await new Promise((r, j) => { ws.onopen = r; ws.onerror = j; }); h.ws = ws; + await until(10000, () => h.info); return h; +} +async function stop(h: Hub) { h.ws.close(); h.proc.kill(); await h.proc.exited; } +async function rpc(h: Hub, m: string, p: object) { const id = ++h.n; h.ws.send(W.encode(JSON.stringify({ id, m, p }))); return until(10000, () => h.replies.get(id)); } +function rows(h: Hub): any[] { return JSON.parse(h.info?.["bots.remote"] ?? "[]"); } +function bot(h: Hub, name: string, state?: string) { return rows(h).find(o => o.name === name && (!state || o.state === state)); } +function signature(secret: string, now: number, body: string) { return "sha256=" + createHmac("sha256", Buffer.from(secret, "hex")).update(`${now}.${body}`).digest("hex"); } +async function signed(h: Hub, peer: string, secret: string, path: string, body?: string) { + const ts = Math.floor(Date.now() / 1000), data = body ?? ""; + return fetch(`http://127.0.0.1:${h.port}${path}`, { method: body === undefined ? "GET" : "POST", body, headers: { "content-type": "application/json", "x-backplane-peer": peer, "x-backplane-timestamp": String(ts), "x-backplane-signature": signature(secret, ts, data) } }); +} +// The third peer is a separate process: every directory connection is +// counted, and hanging sockets stay open until curl's deadline closes them. +const fakePath = join(root, "fake.ts"), modePath = join(root, "mode"), statsPath = join(root, "stats"); +writeFileSync(modePath, "ready"); +writeFileSync(fakePath, `import {readFileSync, writeFileSync} from 'node:fs'; +let requests=0,active=0,max=0; const save=()=>writeFileSync(${JSON.stringify(statsPath)},JSON.stringify({requests,active,max})); +const s=Bun.listen({hostname:'127.0.0.1',port:0,socket:{data(sock,data){ + if(sock.data) return; sock.data=true; const request=new TextDecoder().decode(data); + if(request.startsWith('GET /bots/dir ')){requests++;active++;max=Math.max(max,active);sock.data='dir';save(); + const mode=readFileSync(${JSON.stringify(modePath)},'utf8'); if(mode==='hang') return; + const body=mode==='invalid'?'{}':JSON.stringify([{name:'ghost',look:2,state:'ready'}]); + sock.end('HTTP/1.1 200 OK\\r\\nContent-Length: '+Buffer.byteLength(body)+'\\r\\nConnection: close\\r\\n\\r\\n'+body); + } else sock.end('HTTP/1.1 404 Not Found\\r\\nContent-Length: 0\\r\\nConnection: close\\r\\n\\r\\n'); +},close(sock){if(sock.data==='dir'){active--;save();}},error(){}}});console.log(s.port);`); +const fake = Bun.spawn([process.execPath, fakePath], { stdout: "pipe", stderr: "inherit" }); procs.push(fake); +const reader = fake.stdout.getReader(); const portLine = await reader.read(); const fakePort = Number(new TextDecoder().decode(portLine.value).trim()); reader.releaseLock(); +const stats = () => { try { return JSON.parse(readFileSync(statsPath, "utf8")); } catch { return { requests: 0, active: 0, max: 0 }; } }; +try { + let a = await start("alpha"), b = await start("beta"); + check("real hubs start", a.info && b.info); + check("create healthy bot", (await rpc(b, "bots.create", { name: "miso" }))?.ok); + check("create inviter bot", (await rpc(a, "bots.create", { name: "nori" }))?.ok); + const inv = await rpc(a, "bots.invite", { url: `http://127.0.0.1:${a.port}` }); + const linkedAt = Date.now(); check("link by invite/join", (await rpc(b, "bots.join", { invite: inv?.invite }))?.ok); + if (!baseline) { + check("join refreshes both sides before minute tick", await until(5000, () => bot(a, "miso") && bot(b, "nori")), [rows(a), rows(b)]); + console.log(`MEASURE link_to_both_directories_ms=${Date.now() - linkedAt}`); + } + // A normal signed link from the fake peer; the hub owns the invite and + // its secret exactly as for a real joining hub. + const fi = await rpc(a, "bots.invite", { url: `http://127.0.0.1:${a.port}` }); + const secret = JSON.parse(readFileSync(join(a.home, "secrets", "peers", fi.peer), "utf8")).secret; + check("link TCP fake peer", (await signed(a, fi.peer, secret, "/bots/link", JSON.stringify({ name: "fake", url: `http://127.0.0.1:${fakePort}` }))).ok); + if (!baseline) check("cache fake peer's prior directory", await until(5000, () => bot(a, "ghost", "ready")), rows(a)); + // Leave a whole second between signed empty-body requests so the + // existing replay protection cannot mistake a restart for a replay. + await sleep(1200); writeFileSync(modePath, "hang"); + const aPort = a.port; await stop(a); const beforeRequests = stats().requests; a = await start("alpha", aPort); + const initial = rows(a); + const seen = await until(22000, () => bot(a, "miso", "ready")); + const startMs = Date.now() - a.started; + console.log(`MEASURE mode=${baseline ? "before" : "after"} start_to_info_ms=${a.infoAt - a.started} start_to_healthy_bot_ms=${startMs} fake_requests=${stats().requests - beforeRequests} fake_max_active=${stats().max}`); + check("healthy peer bot appears with hanging TCP peer", seen, rows(a)); + if (!baseline) { + check("startup serves cached rows immediately, away with seen", initial.some(o => o.name === "ghost" && o.state === "away" && o.seen > 0), initial); + check("healthy result publishes before dead peer deadline", startMs < 3500, startMs); + const ghostSeen = initial.find(o => o.name === "ghost")?.seen; + const hungAt = Date.now(); await until(5500, () => stats().active === 0); + console.log(`MEASURE fake_timeout_observed_ms=${Date.now() - a.started} after_healthy_ms=${Date.now() - hungAt}`); + check("dead peer keeps its rows away and its last-seen time", bot(a, "ghost", "away")?.seen === ghostSeen, rows(a)); + check("failed peer never removes healthy peer", bot(a, "miso", "ready"), rows(a)); + check("directory fetch has short total deadline", Date.now() - a.started < 5500, Date.now() - a.started); + check("cache lives outside event log and secrets", existsSync(join(a.home, "bots.cache")) && !readFileSync(join(a.home, "events.jsonl"), "utf8").includes('"seen"'), rows(a)); + // An authenticated return signal while a fetch is active is coalesced. + await sleep(1100); const r0 = stats().requests; + await signed(a, fi.peer, secret, "/bots/deliver", JSON.stringify({ to: "person", from: "ghost", text: "return one" })); + await until(2000, () => stats().requests > r0); + await signed(a, fi.peer, secret, "/bots/deliver", JSON.stringify({ to: "person", from: "ghost", text: "return two" })); + await sleep(200); check("return triggers do not overlap a peer's fetch", stats().requests === r0 + 1 && stats().max === 1, stats()); + await until(5000, () => stats().active === 0); + writeFileSync(modePath, "ready"); await sleep(1100); const returnedAt = Date.now(); + await signed(a, fi.peer, secret, "/bots/deliver", JSON.stringify({ to: "person", from: "ghost", text: "back online" })); + check("peer return refreshes before minute tick", await until(2500, () => bot(a, "ghost", "ready")), rows(a)); + console.log(`MEASURE return_to_directory_ms=${Date.now() - returnedAt}`); + writeFileSync(modePath, "invalid"); await sleep(1100); + // Restart to trigger a sweep against an invalid successful JSON body. + await stop(a); a = await start("alpha", aPort); + await until(2000, () => stats().active === 0 && bot(a, "miso", "ready")); + check("invalid JSON shape retains cached peer away", bot(a, "ghost", "away"), rows(a)); + const un = await rpc(a, "bots.unpeer", { peer: fi.peer }); + check("revoking peer removes cached directory", un?.ok && await until(2000, () => !bot(a, "ghost")), rows(a)); + await sleep(1100); await stop(a); a = await start("alpha", aPort); + check("revoked cache stays gone across restart", !bot(a, "ghost"), rows(a)); + } + await stop(a); await stop(b); +} catch (e) { failed = true; console.error(e); } +finally { for (const p of procs) { if (p.exitCode === null) { p.kill(); await p.exited; } } rmSync(root, { recursive: true, force: true }); } +process.exit(failed ? 1 : 0); diff --git a/test/tools/bots_e2e.ts b/test/tools/bots_e2e.ts index ba6500a650..c96f46c41c 100644 --- a/test/tools/bots_e2e.ts +++ b/test/tools/bots_e2e.ts @@ -220,8 +220,8 @@ try { check("no credentials is refused (401) and says so", bn.status === 401 && bn.text.includes("no credentials"), bn); const bsig = await post(url, body, { authorization: `Bearer ${token}`, "x-backplane-timestamp": String(now()), "x-backplane-signature": sign("00".repeat(32), now(), body) }); check("a wrong signature is refused even with the token (401)", bsig.status === 401 && bsig.text.includes("signature"), bsig); - const bbig = await postForm(url, form({ transcription: "big" }, new Uint8Array(300000)), { authorization: `Bearer ${token}` }); - check("a form over 256 KB is refused (413)", bbig.status === 413, bbig.status); + const bbig = await postForm(url, form({ transcription: "big" }, new Uint8Array(2100000)), { authorization: `Bearer ${token}` }); + check("a form over 2 MB is refused (413)", bbig.status === 413, bbig.status); const tk3 = await rpc(a, "bots.hook_token", { hook: hk.hook, op: "new" }); check("a new token replaces the old one", tk3?.ok && /^[0-9a-f]{32}$/.test(tk3.token) && tk3.token !== token, tk3); const bold = await postForm(url, form({ transcription: "old token" }), { authorization: `Bearer ${token}` }); diff --git a/test/tools/far_e2e.ts b/test/tools/far_e2e.ts new file mode 100644 index 0000000000..42378f074d --- /dev/null +++ b/test/tools/far_e2e.ts @@ -0,0 +1,331 @@ +// End to end: threads elsewhere (far.bend, docs/bots.md "Threads +// elsewhere") on two headless hubs linked to each other. Each hub lists the +// other as one of the owner's machines (BACKPLANE_PEERS), so each shares +// its projects, bots and threads with the other. Checks that a bot made on +// alpha appears on beta, mirrored and named by the link; that writing to +// it from beta reaches alpha and alpha's answer comes back; that a client +// joining beta later gets the mirror whole; that when alpha stops, beta +// keeps it, away; and that beta, restarted while alpha is down, shows it +// from disk, away. Agents are stand-ins (claude answers "pong from the +// bot"). Prints "ok ..." / "FAIL ..." lines and timings. +// +// bun test/tools/far_e2e.ts [BINARY] [WIREDIR] +// +// BINARY defaults to build/backplane; WIREDIR to build/wire (the hub's +// CBOR codec: bend test/wire/index.html -o build/wire). With +// FAR_E2E_DUMP=DIR, beta's log and the messages a client got (the +// directory, the mirror) are written there for test/native/ui_snap.bend. +import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync, chmodSync, mkdirSync, existsSync, copyFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { createHmac } from "node:crypto"; + +const args = process.argv.slice(2); +const [bin = "build/backplane", wire = "build/wire"] = args.filter((a) => !a.startsWith("--")); +for (const f of readdirSync(wire).filter((f) => f.endsWith(".js"))) (0, eval)(readFileSync(`${wire}/${f}`, "utf8")); +const W = (globalThis as any).Wire; + +let failed = false; +const check = (name: string, ok: boolean, got?: unknown) => { + console.log(ok ? `ok ${name}` : `FAIL ${name}: ${JSON.stringify(got)?.slice(0, 600)}`); + if (!ok) failed = true; +}; +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); +function freePort(): number { + const s = Bun.listen({ hostname: "127.0.0.1", port: 0, socket: { data() {} } }); + const p = s.port; + s.stop(true); + return p; +} + +const root = mkdtempSync(join(tmpdir(), "bp-far-e2e-")); +const fake = join(root, "bin"); +mkdirSync(fake); +const answer = "pong from the bot"; +const claude = `#!/bin/sh +n=0 +while IFS= read -r line; do + case "$line" in + *'"type":"user"'*) + case "$line" in *'hold until stopped'*) + printf '%s\\n' '{"type":"assistant","message":{"id":"hold","content":[{"type":"text","text":"waiting for Stop"}]}}' + continue ;; + esac + n=$((n + 1)) + printf '%s\\n' '{"type":"assistant","message":{"id":"m'"$$-$n"'","content":[{"type":"text","text":"${answer}"}]}}' + printf '%s\\n' '{"type":"result","is_error":false,"result":"${answer}"}' ;; + esac +done +`; +for (const n of ["claude", "codex", "grok"]) { + writeFileSync(join(fake, n), n === "claude" ? claude : "#!/bin/sh\nexit 1\n"); + chmodSync(join(fake, n), 0o755); +} + +type Hub = { name: string; home: string; port: number; peers: string; proc: ReturnType | null }; +type Client = { ws: WebSocket; seen: any[]; far: any[]; replies: Map; n: number; info: any; raw: string[] }; + +function spawn(h: Hub) { + h.proc = Bun.spawn([resolve(bin), "--home", h.home, "--port", String(h.port), "--no-tailscale"], { + env: { ...process.env, DISPLAY: "", WAYLAND_DISPLAY: "", BACKPLANE_NAME: h.name, BACKPLANE_NO_UPDATE: "1", BACKPLANE_PEERS: h.peers, PATH: `${fake}:/usr/bin:/bin` }, + stdout: "ignore", + stderr: "ignore", + }); +} + +async function up(h: Hub) { + for (let i = 0; i < 200; i++) { + try { + const r = await fetch(`http://127.0.0.1:${h.port}/hello`); + if (r.status === 200) return; + } catch {} + await sleep(100); + } + throw new Error(`${h.name} did not start`); +} + +async function connect(h: Hub): Promise { + const c: Client = { ws: null as any, seen: [], far: [], replies: new Map(), n: 0, info: {}, raw: [] }; + const ws = new WebSocket(`ws://127.0.0.1:${h.port}/ws`); + ws.binaryType = "arraybuffer"; + ws.onmessage = (e) => { + const text = W.decode(new Uint8Array(e.data as ArrayBuffer)); + const o = JSON.parse(text); + if (o.t === "far") c.raw.push(text); + for (const x of o.items ?? []) c.seen.push(x); + if (o.t === "far") c.far.push({ ...o, at: Date.now() }); + if (o.t === "reply") c.replies.set(Number(o.id), o); + if (o.info) c.info = { ...c.info, ...o.info }; + }; + await new Promise((r, j) => { ws.onopen = r; ws.onerror = j; }); + c.ws = ws; + return c; +} + +async function until(ms: number, f: () => T | undefined | null | false): Promise { + const end = Date.now() + ms; + for (;;) { + const v = f(); + if (v) return v; + if (Date.now() > end) return undefined; + await sleep(50); + } +} + +async function rpc(c: Client, m: string, p: object): Promise { + const id = ++c.n; + c.ws.send(W.encode(JSON.stringify({ id, m, p }))); + return await until(20000, () => c.replies.get(id)); +} + +// every change a client has from a link, oldest first +const farItems = (c: Client, link: string) => c.far.filter((o) => o.link === link).flatMap((o) => o.items.map((i: any) => i.c)); +const lastFar = (c: Client, link: string) => c.far.filter((o) => o.link === link).at(-1); + +const pa = freePort(); +const pb = freePort(); +const a: Hub = { name: "alpha", home: join(root, "alpha"), port: pa, peers: `http://127.0.0.1:${pb}`, proc: null }; +const b: Hub = { name: "beta", home: join(root, "beta"), port: pb, peers: `http://127.0.0.1:${pa}`, proc: null }; +const clients: Client[] = []; +try { + spawn(a); + spawn(b); + await up(a); + await up(b); + const ca = await connect(a); + const cb = await connect(b); + clients.push(ca, cb); + // each lists the other among the owner's machines (it answered /hello; + // the list is probed every 2 minutes) + const knows = (c: Client, port: number) => String(c.info?.machines ?? "").includes(`127.0.0.1:${port}`); + const tm = Date.now(); + const both = await until(130000, () => knows(ca, pb) && knows(cb, pa)); + check("each hub lists the other as the owner's", !!both, [ca.info?.machines, cb.info?.machines]); + console.log(` machines lists ready after ${Date.now() - tm} ms`); + + // link them (the invite's address is alpha's) + const inv = await rpc(ca, "bots.invite", { url: `http://127.0.0.1:${pa}` }); + const joined = await rpc(cb, "bots.join", { invite: inv?.invite ?? "" }); + check("linked", !!(inv?.ok && joined?.ok), [inv, joined]); + const peer = await until(8000, () => cb.seen.find((c) => c.$ === "PeerSet" && !c.revoked)); + const link = peer?.id as string; + check("beta names the link", typeof link === "string" && link.length > 0, peer); + + // a bot on alpha appears on beta, mirrored + const t0 = Date.now(); + const miso = await rpc(ca, "bots.create", { name: "miso", persona: "a test bot" }); + check("bots.create on alpha", !!miso?.ok, miso); + // (the first sync can wait for the machines list and the minute tick; a + // push arrives as soon as both know each other as the owner's) + const got = await until(90000, () => farItems(cb, link).find((c) => c.$ === "BotSet" && c.name === "miso")); + const tMirror = Date.now() - t0; + check("beta gets alpha's bot, in alpha's ids, from its link", !!got && got.id === miso?.bot, got); + const th = got?.thread as string; + check("and the bot's thread", !!farItems(cb, link).find((c) => c.$ === "ThreadCreated" && c.id === th), farItems(cb, link).map((c) => c.$)); + console.log(` mirror of a new bot reached beta's client in ${tMirror} ms`); + check("never a setting, a peer or a device", !farItems(cb, link).some((c) => ["SettingSet", "PeerSet", "DeviceRegistered", "HookSet", "RoomPosted"].includes(c.$)), + farItems(cb, link).map((c) => c.$)); + + // beta writes to it: the request goes to alpha in alpha's ids, and the answer comes back + const t1 = Date.now(); + const sent = await rpc(cb, "turn.start", { thread: `${link}~${th}`, text: "hello from beta", msg: "far-m1", mode: "queue" }); + check("turn.start on the far thread is answered ok", !!sent?.ok, sent); + const onA = await until(10000, () => ca.seen.find((c) => c.$ === "MessagePosted" && c.thread === th && c.text.includes("hello from beta"))); + check("alpha's thread gets beta's message", !!onA, ca.seen.filter((c) => c.$ === "MessagePosted")); + const back = await until(20000, () => farItems(cb, link).find((c) => c.$ === "MessagePosted" && c.thread === th && c.text === answer)); + check("alpha's answer comes back to beta", !!back, farItems(cb, link).filter((c) => c.$ === "MessagePosted")); + console.log(` beta's message to alpha's answer mirrored on beta: ${Date.now() - t1} ms`); + const mine = farItems(cb, link).filter((c) => c.$ === "MessagePosted" && c.thread === th && c.text.includes("hello from beta")); + check("beta sees its own message once, with its msg id", mine.length === 1 && mine[0].msg === "far-m1", mine); + + // Ordinary project threads use the same mirror and routing path. + const projectPath = join(root, "board"); + mkdirSync(projectPath); + const project = await rpc(ca, "project.add", { path: projectPath }); + const pc = await until(5000, () => ca.seen.find((c) => c.$ === "ProjectCreated" && c.root === projectPath)); + const ordinary = await rpc(ca, "thread.create", { project: pc?.id ?? project?.id, title: "REMOTE BOARD REVIEW" }); + const created = await until(5000, () => ca.seen.find((c) => c.$ === "ThreadCreated" && c.project === pc?.id && c.title === "REMOTE BOARD REVIEW")); + const pt = created?.id; + check("ordinary project thread created", !!ordinary?.ok && !!pt, [ordinary, created]); + const catalog = await until(8000, () => farItems(cb, link).find((c) => c.$ === "ThreadCreated" && c.id === pt)); + check("ordinary project and thread are mirrored together", !!catalog && catalog.project === pc?.id && farItems(cb, link).some((c) => c.$ === "ProjectCreated" && c.id === pc?.id), catalog); + const psent = await rpc(cb, "turn.start", { thread: `${link}~${pt}`, text: "review from beta", msg: "ordinary-m1", mode: "queue" }); + check("ordinary thread send reaches its owner", !!psent?.ok && !!await until(8000, () => ca.seen.find((c) => c.$ === "MessagePosted" && c.thread === pt && c.text.includes("review from beta"))), psent); + check("ordinary thread answer returns through the mirror", !!await until(8000, () => farItems(cb, link).find((c) => c.$ === "MessagePosted" && c.thread === pt && c.text === answer))); + + // Stop goes to the owner's live agent, not a process on the mirror. + await rpc(cb, "turn.start", { thread: `${link}~${th}`, text: "hold until stopped", msg: "far-hold", mode: "queue" }); + const running = await until(8000, () => ca.seen.find((c) => c.$ === "MessagePosted" && c.msg === "far-hold")); + check("remote hold starts on alpha", !!running); + const stopped = await rpc(cb, "turn.interrupt", { thread: `${link}~${th}` }); + const stopOnA = await until(8000, () => ca.seen.find((c) => c.$ === "TurnChanged" && c.thread === th && c.state === "interrupted")); + check("remote Stop reaches alpha", !!stopped?.ok && !!stopOnA, [stopped, stopOnA]); + check("remote Stop is mirrored", !!await until(8000, () => farItems(cb, link).find((c) => c.$ === "TurnChanged" && c.thread === th && c.state === "interrupted"))); + + // A real approval waits on alpha; beta answers the namespaced ask. + const approval = fetch(`http://127.0.0.1:${a.port}/mcp/${th}`, { + method: "POST", headers: { "content-type": "application/json" }, + body: JSON.stringify({ jsonrpc: "2.0", id: 9, method: "tools/call", params: { name: "project_create", arguments: { path: join(root, "declined-project") } } }), + }).then((r) => r.json()); + const ask = await until(8000, () => farItems(cb, link).find((c) => c.$ === "AskOpened" && c.tool === "project_create")); + check("alpha's approval appears on beta", !!ask, ask); + const approved = await rpc(cb, "ask.answer", { id: `${link}~${ask?.id}`, answer: "decline" }); + const approvalResult = await approval; + check("remote approval response reaches alpha", !!approved?.ok && !!await until(8000, () => ca.seen.find((c) => c.$ === "AskClosed" && c.id === ask?.id && c.answer === "decline")), [approved, approvalResult]); + check("declined approval does not create a folder", !existsSync(join(root, "declined-project"))); + + // a request beta may not send on + const del = await rpc(cb, "thread.delete", { thread: `${link}~${th}` }); + check("a delete of a far thread is refused", !!del && !del.ok, del); + check("and alpha's thread is still there", !ca.seen.some((c) => c.$ === "ThreadDeleted" && c.id === th)); + + // a client joining later gets the mirror whole + const t2 = Date.now(); + const cb2 = await connect(b); + clients.push(cb2); + const snap = await until(5000, () => lastFar(cb2, link)); + console.log(` a new client on beta has the mirror ${Date.now() - t2} ms after connecting`); + check("a new client gets the mirror whole", !!snap && snap.since === 0 && snap.items.some((i: any) => i.c.$ === "BotSet") && !snap.away, snap && { ...snap, items: snap.items.length }); + const dupes = new Set(); + let twice = false; + for (const i of snap?.items ?? []) { if (dupes.has(i.n)) twice = true; dupes.add(i.n); } + check("each item once", !twice); + check("the mirror is kept on disk", existsSync(join(b.home, "far", `${link}.jsonl`))); + const dump = process.env.FAR_E2E_DUMP; + if (dump) { + mkdirSync(dump, { recursive: true }); + copyFileSync(join(b.home, "events.jsonl"), join(dump, "events.jsonl")); + const info = JSON.stringify({ t: "info", info: { "bots.remote": cb.info["bots.remote"] ?? "[]" } }); + writeFileSync(join(dump, "info.txt"), [info, ...cb2.raw].join("\n") + "\n"); + writeFileSync(join(dump, "ids.txt"), `${link}~${th}\n${link}~${miso?.bot}\n${link}~${pt}\n`); + copyFileSync(join(a.home, "events.jsonl"), join(dump, "owner-events.jsonl")); + mkdirSync(join(dump, "far"), { recursive: true }); + copyFileSync(join(b.home, "far", `${link}.jsonl`), join(dump, "far", `${link}.jsonl`)); + } + + // alpha stops: beta keeps it, away (within the minute's directory) + const t3 = Date.now(); + a.proc!.kill(); + await a.proc!.exited; + a.proc = null; + const away = await until(75000, () => { const f = lastFar(cb, link); return f && f.away ? f : undefined; }); + check("beta marks alpha away", !!away, lastFar(cb, link)); + console.log(` alpha away on beta ${Date.now() - t3} ms after it stopped`); + const cb3 = await connect(b); + clients.push(cb3); + const stale = await until(5000, () => lastFar(cb3, link)); + check("still listed while away", !!stale && stale.away && stale.items.some((i: any) => i.c.$ === "BotSet" && i.c.name === "miso"), stale && { ...stale, items: stale.items.length }); + const refused = await rpc(cb3, "turn.start", { thread: `${link}~${th}`, text: "are you there", msg: "far-m2", mode: "queue" }); + check("writing to it while away says so", !!refused && !refused.ok && String(refused.error).includes("away"), refused); + + // beta restarts while alpha is down: the mirror comes back from disk, away + for (const c of [cb, cb2, cb3]) c.ws.close(); + b.proc!.kill(); + await b.proc!.exited; + spawn(b); + await up(b); + const cb4 = await connect(b); + clients.push(cb4); + const disk = await until(8000, () => lastFar(cb4, link)); + check("after a restart the mirror shows from disk, away", !!disk && disk.away && disk.items.some((i: any) => i.c.$ === "BotSet"), disk && { ...disk, items: disk.items.length }); + // Finish beta's first discovery while alpha is still down. This makes + // the initial owner-return hint arrive before beta recognizes alpha, + // rather than letting startup timing accidentally hide that case. + const absent = await until(10000, () => cb4.info?.machines && !String(cb4.info.machines).includes(`127.0.0.1:${a.port}`)); + check("restart discovery observes the stopped owner", !!absent, cb4.info?.machines); + + // alpha comes back: beta pulls and is here again + spawn(a); + await up(a); + const ca2 = await connect(a); + clients.push(ca2); + const catchup = await rpc(ca2, "turn.start", { thread: th, text: "after partition", msg: "catchup-m1", mode: "queue" }); + check("owner can post after return", !!catchup?.ok, catchup); + const here = await until(75000, () => { const f = lastFar(cb4, link); return f && !f.away ? f : undefined; }); + check("alpha back: here again", !!here, lastFar(cb4, link)); + check("missed message catches up after return", !!await until(10000, () => farItems(cb4, link).find((c) => c.$ === "MessagePosted" && c.msg === "catchup-m1"))); + + // Replace only this temporary owner's history. A new epoch must clear + // its old mirror even though the replacement has a lower sequence. + const oldEpoch = lastFar(cb4, link)?.epoch; + ca2.ws.close(); + a.proc!.kill(); + await a.proc!.exited; + const peerChange = ca.seen.find((c) => c.$ === "PeerSet" && c.id === link); + const resetAt = Math.floor(Date.now() / 1000) + 1; + const replacement = [ + { ...peerChange, at: resetAt }, + { $: "ThreadCreated", id: "reset-thread", project: "", title: "New history", env: "local", provider: "claude", at: resetAt }, + { $: "BotSet", id: "reset-bot", name: "new-history", thread: "reset-thread", look: 2, persona: "", at: resetAt }, + ]; + check("replacement keeps the linked peer", !!peerChange, peerChange); + writeFileSync(join(a.home, "events.jsonl"), replacement.map((c) => JSON.stringify(c)).join("\n") + "\n"); + spawn(a); + await up(a); + const ca3 = await connect(a); + clients.push(ca3); + const resetTurn = await rpc(ca3, "turn.start", { thread: "reset-thread", text: "new history", msg: "reset-message", mode: "queue" }); + check("replacement owner accepts a new turn", !!resetTurn?.ok && !!await until(5000, () => ca3.seen.find((c) => c.$ === "MessagePosted" && c.msg === "reset-message")), resetTurn); + const reset = await until(75000, () => cb4.far.find((f) => f.link === link && f.epoch && f.epoch !== oldEpoch && f.items.some((i: any) => i.c.$ === "BotSet" && i.c.id === "reset-bot"))); + check("history replacement takes its lower sequence", !!reset && reset.head < disk.head, + reset ? { epoch: reset.epoch, head: reset.head, old: disk.head } : { machines: { owner: ca3.info?.machines, mirror: cb4.info?.machines }, last: lastFar(cb4, link), owner: ca3.seen.slice(-8) }); + const mirrorPath = join(b.home, "far", `${link}.jsonl`); + check("history replacement clears old disk projection", !!reset && !readFileSync(mirrorPath, "utf8").includes(miso.bot)); + + // A late old push is only a hint to pull the current authority. It + // cannot put the old history back, or advance the new mirror to 999. + const secret = JSON.parse(readFileSync(join(a.home, "secrets", "peers", link), "utf8")).secret; + const delayed = JSON.stringify({ name: "alpha", epoch: oldEpoch, since: 0, head: 999, items: [{ n: 999, c: { $: "BotSet", id: miso.bot, name: "stale-history", thread: th, look: 1, at: resetAt } }] }); + const ts = Math.floor(Date.now() / 1000); + const sig = "sha256=" + createHmac("sha256", Buffer.from(secret, "hex")).update(`${ts}.${delayed}`).digest("hex"); + const late = await fetch(`http://127.0.0.1:${b.port}/far/push`, { method: "POST", body: delayed, + headers: { "content-type": "application/json", "x-backplane-peer": link, "x-backplane-timestamp": String(ts), "x-backplane-signature": sig } }); + await sleep(500); + check("late push cannot restore an old epoch", late.ok && lastFar(cb4, link)?.epoch === reset?.epoch && lastFar(cb4, link)?.head < 999 && !readFileSync(mirrorPath, "utf8").includes("stale-history")); +} finally { + for (const c of clients) { try { c.ws.close(); } catch {} } + for (const h of [a, b]) if (h.proc) { h.proc.kill(); await h.proc.exited; } + rmSync(root, { recursive: true, force: true }); +} +console.log(failed ? "far e2e: FAIL" : "far e2e: ok"); +process.exit(failed ? 1 : 0);