Skip to content

Commit a799fde

Browse files
author
i2cjak
authored
Merge pull request #37 from i2cjak/backplane/cross-machine-rooms
Shared rooms across machines
2 parents 279e8b7 + 9bcfa4c commit a799fde

6 files changed

Lines changed: 336 additions & 8 deletions

File tree

‎LAWS.bend‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,25 @@ law bot_hop_bounded:
22512251
for +h: Nat
22522252
{BOT.Hop.bound(BOT.Hop.next(h)) == True{} : Bool}
22532253

2254+
# a linked machine never takes over a live room here with no member on it
2255+
law bot_room_no_takeover:
2256+
for +i: String
2257+
for +n: String
2258+
for +c: Nat
2259+
for +s: M.State
2260+
for +pid: String
2261+
{BHS.Bots.room.open(BOT.Room{i, n, Nil{}, 1n+c, False{}}, s, pid) == False{} : Bool}
2262+
2263+
# nor opens a room the person deleted
2264+
law bot_room_deleted_stays:
2265+
for +i: String
2266+
for +n: String
2267+
for +ms: List<&2, String>
2268+
for +c: Nat
2269+
for +s: M.State
2270+
for +pid: String
2271+
{BHS.Bots.room.open(BOT.Room{i, n, ms, 1n+c, True{}}, s, pid) == False{} : Bool}
2272+
22542273
# a cat waits for you (ears up) whenever its thread needs an approval
22552274
law bot_mood_wait:
22562275
for talked: Bool

‎PROOF.bend‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4427,6 +4427,12 @@ def Laws.bot_hop_step(ok, n, h):
44274427
def Laws.bot_hop_bounded(h):
44284428
Laws.bot_hop_step(Nat.is_le(1n+h, BOT.Hop.max()), 1n+h, {==})
44294429

4430+
def Laws.bot_room_no_takeover(i, n, c, s, pid):
4431+
{==}
4432+
4433+
def Laws.bot_room_deleted_stays(i, n, ms, c, s, pid):
4434+
{==}
4435+
44304436
def Laws.bot_mood_wait(talked, idle):
44314437
{==}
44324438

‎docs/bots.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ than the hop of the turn that sent it. A bot never sends past
7676
- Delivery to a local bot goes through the thread inbox (`Hub.inbox`): it
7777
queues behind a running turn, never interrupts.
7878

79+
### Shared rooms
80+
81+
A room's members can be on other machines (`name@machine`). A post goes to
82+
each linked machine once (`Bots.remotes`), with the room's id, name, the
83+
bots there it is for (`tos`), and its members as that machine names them:
84+
a bare name is a bot there, `name@` a bot on the sender, `name@machine` a
85+
bot on a third machine. The receiving machine keeps the room under the
86+
same id (a `RoomSet` when it is new or its members changed), logs the post
87+
once, and wakes the bots it is for (`Bots.shared`), so its bots and person
88+
see the room and can post back. A machine sends its own posts to every
89+
other and never passes on one it received, so nothing loops; a member on a
90+
machine the poster is not linked to does not hear that post.
91+
92+
A linked machine may post in a room here only when the room is new here or
93+
has a member on that machine (law `bot_room_no_takeover`), and never opens
94+
a room the person deleted (law `bot_room_deleted_stays`). The other
95+
machine learns of a room at its first post.
96+
7997
## Machines and people
8098

8199
A peer is another hub, linked by an invite: the inviting hub makes

‎src/core/bothub.bend‎

Lines changed: 224 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,93 @@ def Bots.remote.body(+from: String, +to: String, +room: String, +rname: String,
8888
J.Json.show(J.Json.obj([J.KV{"from", J.Str{from}}, J.KV{"to", J.Str{to}}, J.KV{"room", J.Str{room}}, J.KV{"roomName", J.Str{rname}},
8989
J.KV{"text", J.Str{text}}, J.KV{"hop", J.Json.nat(hop)}]))
9090

91-
def Bots.remote.out(m: Maybe<&2, BT.Peer>, +from: String, +to: String, +room: String, +rname: String, +text: String, +hop: Nat) -> List<&2, B.Out>:
91+
# Shared rooms: a room may have members on other machines. A post goes to
92+
# each linked machine once, naming the room, its members as that machine
93+
# should read them, and which of its bots hear it ("tos"); the machine
94+
# keeps the room too, so its bots can post back. Each machine sends its own
95+
# posts to every other: a machine never passes on a post it received.
96+
97+
def Bots.peer.idm(m: Maybe<&2, BT.Peer>) -> String:
9298
match m:
9399
case Some{p}:
94-
[B.BotFx{"send", Bots.peer.id(p), "/bots/deliver", Bots.remote.body(from, Bots.at.name(to), room, rname, text, hop)}]
100+
Bots.peer.id(p)
101+
case None{}:
102+
""
103+
104+
def Bots.peer.namem(m: Maybe<&2, BT.Peer>, +d: String) -> String:
105+
match m:
106+
case Some{BT.Peer{_, +n, _, _, _}}:
107+
Bool.pick(String, String.is_empty(n), d, n)
95108
case None{}:
109+
d
110+
111+
def Bots.member.peer.k(remote: Bool, +s: M.State, +m: String) -> String:
112+
match remote:
113+
case True{}:
114+
Bots.peer.idm(Bots.peer(s, Bots.at.peer(m)))
115+
case False{}:
116+
""
117+
118+
# the linked machine a member is on: its peer id, or "" for a bot here or
119+
# on a machine not linked
120+
def Bots.member.peer(+s: M.State, +m: String) -> String:
121+
Bots.member.peer.k(Bots.remote(m), s, m)
122+
123+
def Bots.peers.add(+p: String, +acc: List<&2, String>) -> List<&2, String>:
124+
Bool.pick(List<&2, String>, Bool.or(String.is_empty(p), BT.Room.has(acc, p)), acc, p <> acc)
125+
126+
# the linked machines a room's members are on, each once
127+
def Bots.peers.of(ms: List<&2, String>, +s: M.State, acc: List<&2, String>) -> List<&2, String>:
128+
match ms:
129+
case Nil{}:
130+
acc
131+
case +m <> t:
132+
Bots.peers.of(t, s, Bots.peers.add(Bots.member.peer(s, m), acc))
133+
134+
# the names of a room's members on the machine p
135+
def Bots.on.peer(ms: List<&2, String>, +s: M.State, +p: String) -> List<&2, String>:
136+
match ms:
137+
case Nil{}:
96138
Nil{}
139+
case +m <> t:
140+
BT.Post.keep(String, String.eq(Bots.member.peer(s, m), p), Bots.at.name(m), Bots.on.peer(t, s, p))
141+
142+
def Bots.member.for.k(remote: Bool, there: Bool, +s: M.State, +m: String) -> String:
143+
match remote there:
144+
case True{} True{}:
145+
Bots.at.name(m)
146+
case True{} False{}:
147+
Bots.at.name(m) ++ "@" ++ Bots.peer.namem(Bots.peer(s, Bots.at.peer(m)), Bots.at.peer(m))
148+
case False{} _:
149+
BT.Bot.name(Bots.bot(s, m)) ++ "@"
150+
151+
# a member as the machine p reads it: a bot there by its bare name, a bot
152+
# here as "name@" (the sender's machine), a bot elsewhere as "name@machine"
153+
def Bots.member.for(+s: M.State, +p: String, +m: String) -> String:
154+
Bots.member.for.k(Bots.remote(m), String.eq(Bots.member.peer(s, m), p), s, m)
155+
156+
def Bots.members.for(ms: List<&2, String>, +s: M.State, +p: String) -> List<&2, String>:
157+
match ms:
158+
case Nil{}:
159+
Nil{}
160+
case +m <> t:
161+
Bots.member.for(s, p, m) <> Bots.members.for(t, s, p)
162+
163+
# a room post for a linked machine; "to" is the first bot for machines that
164+
# read only that
165+
def Bots.shared.body(+from: String, +tos: List<&2, String>, +ms: List<&2, String>, +room: String, +rname: String, +text: String, +hop: Nat) -> String:
166+
J.Json.show(J.Json.obj([J.KV{"from", J.Str{from}}, J.KV{"to", J.Str{Maybe.default(&2, String, List.head(&2, String, tos), "")}},
167+
J.KV{"tos", J.Json.arr(MC.Tool.strs(tos))}, J.KV{"members", J.Json.arr(MC.Tool.strs(ms))}, J.KV{"room", J.Str{room}}, J.KV{"roomName", J.Str{rname}},
168+
J.KV{"text", J.Str{text}}, J.KV{"hop", J.Json.nat(hop)}]))
169+
170+
# one delivery for each linked machine a room's members are on
171+
def Bots.remotes(ps: List<&2, String>, +s: M.State, +ms: List<&2, String>, +from: String, +room: String, +rname: String, +text: String, +hop: Nat) -> List<&2, B.Out>:
172+
match ps:
173+
case Nil{}:
174+
Nil{}
175+
case +p <> t:
176+
B.BotFx{"send", p, "/bots/deliver", Bots.shared.body(from, Bots.on.peer(ms, s, p), Bots.members.for(ms, s, p), room, rname, text, hop)} <>
177+
Bots.remotes(t, s, ms, from, room, rname, text, hop)
97178

98179
def Bots.member.local(b: Maybe<&2, BT.Bot>, +s: M.State, +now: Nat, +seq: Nat, +i: Nat, +from: String, +fname: String, +room: String, +rname: String, +text: String, +hop: Nat) -> B.Step:
99180
match b:
@@ -102,11 +183,12 @@ def Bots.member.local(b: Maybe<&2, BT.Bot>, +s: M.State, +now: Nat, +seq: Nat, +
102183
case None{}:
103184
B.Step.none()
104185

105-
# one member of a room hears a post (never its sender)
186+
# one member of a room here hears a post (never its sender; members
187+
# elsewhere hear it through Bots.remotes)
106188
def Bots.member.k(remote: Bool, +s: M.State, +now: Nat, +seq: Nat, +i: Nat, +from: String, +fname: String, +room: String, +rname: String, +text: String, +hop: Nat, +m: String) -> B.Step:
107189
match remote:
108190
case True{}:
109-
B.Step.outs(Bots.remote.out(Bots.peer(s, Bots.at.peer(m)), fname, m, room, rname, text, hop))
191+
B.Step.none()
110192
case False{}:
111193
Bots.member.local(BT.World.find.id(Bots.bot(s, m)), s, now, seq, i, from, fname, room, rname, text, hop)
112194

@@ -132,7 +214,8 @@ def Bots.fan(ms: List<&2, String>, +i: Nat, +s: M.State, +now: Nat, +seq: Nat, +
132214
def Bots.post(+s: M.State, +now: Nat, +from: String, +fname: String, +self: String, r: BT.Room, +text: String, +hop: Nat) -> B.Step:
133215
match r:
134216
case BT.Room{+room, +rname, +ms, _, _}:
135-
B.Step.before([M.RoomPosted{B.Hub.id("p", now, B.Hub.seq(s)), room, fname, text, hop, now}], Nil{},
217+
B.Step.before([M.RoomPosted{B.Hub.id("p", now, B.Hub.seq(s)), room, fname, text, hop, now}],
218+
Bots.remotes(Bots.peers.of(ms, s, Nil{}), s, ms, fname, room, rname, text, hop),
136219
Bots.fan(ms, 0n, s, now, B.Hub.seq(s), from, fname, self, room, rname, text, hop))
137220

138221
# a direct message from a bot (or a person) to one bot, here or elsewhere
@@ -464,11 +547,79 @@ def Bots.ring.k(h: BT.Hook, test: Bool, +s: M.State, +now: Nat, +text: String) -
464547
def Bots.ring(+s: M.State, +now: Nat, +hook: String, +body: String) -> B.Step:
465548
Bots.ring.k(BT.World.hook(M.State.bots(s), hook), RG.Ring.test(body), s, now, RG.Ring.text(body))
466549

467-
# a message from a bot on a linked machine to a bot here
468-
def Bots.incoming(+s: M.State, +now: Nat, +peer: String, +o: J.Json) -> B.Step:
550+
# a direct message from a bot on a linked machine to a bot here
551+
def Bots.incoming.dm(+s: M.State, +now: Nat, +peer: String, +o: J.Json) -> B.Step:
469552
Bots.incoming.k(BT.World.find(M.State.bots(s), J.Json.get_str(o, "to", "")), s, now, peer, J.Json.get_str(o, "from", "") ++ "@" ++ peer,
470553
J.Json.get_str(o, "roomName", ""), J.Json.get_str(o, "text", ""), J.Json.get_nat(o, "hop", 0n))
471554

555+
def Bots.named(xs: List<&2, String>) -> List<&2, String>:
556+
match xs:
557+
case Nil{}:
558+
Nil{}
559+
case +x <> t:
560+
BT.Post.keep(String, Bool.not(String.is_empty(Bots.at.name(x))), x, Bots.named(t))
561+
562+
def Bots.room.cap() -> Nat:
563+
64n
564+
565+
def Bots.strs(xs: List<&2, J.Json>) -> List<&2, String>:
566+
match xs:
567+
case Nil{}:
568+
Nil{}
569+
case x <> t:
570+
J.Json.as_str(Some{x}, "") <> Bots.strs(t)
571+
572+
def Bots.tos.k(xs: List<&2, String>, +to: String) -> List<&2, String>:
573+
match xs:
574+
case Nil{}:
575+
[to]
576+
case x <> t:
577+
x <> t
578+
579+
# the bots here a post is for ("tos", else "to")
580+
def Bots.tos(+o: J.Json) -> List<&2, String>:
581+
Bots.tos.k(Bots.strs(List.take(&2, J.Json, J.Json.elems(J.Json.get_obj(o, "tos")), Bots.room.cap())), J.Json.get_str(o, "to", ""))
582+
583+
def Bots.on.any(ms: List<&2, String>, +s: M.State, +pid: String) -> Bool:
584+
match ms:
585+
case Nil{}:
586+
False{}
587+
case +m <> t:
588+
Bool.or(String.eq(Bots.member.peer(s, m), pid), Bots.on.any(t, s, pid))
589+
590+
# may a linked machine post in (and set the members of) the room here with
591+
# this id: a room new here, or a live one with a member on that machine. A
592+
# machine never takes over a room it has no member in, nor opens one the
593+
# person deleted
594+
def Bots.room.open(r: BT.Room, +s: M.State, +pid: String) -> Bool:
595+
match r:
596+
case BT.Room{_, _, +ms, +c, d}:
597+
Bool.or(Nat.is_eq(c, 0n), Bool.and(Bool.not(d), Bool.and(Bots.on.any(ms, s, pid), Bool.not(String.is_empty(pid)))))
598+
599+
def Bots.room.same(r: BT.Room, +rname: String, +shown: String) -> Bool:
600+
match r:
601+
case BT.Room{_, n, ms, _, d}:
602+
Bool.and(Bool.not(d), Bool.and(String.eq(n, rname), String.eq(BT.Room.members.show(ms), shown)))
603+
604+
def Bots.shared.one(ok: Bool, b: Maybe<&2, BT.Bot>, +s: M.State, +now: Nat, +i: Nat, +from: String, +room: String, +rname: String, +text: String, +hop: Nat) -> B.Step:
605+
match ok b:
606+
case True{} Some{+bot}:
607+
Bots.wake(s, now, "", bot, Bots.msg(now, B.Hub.seq(s), i), Bots.note(from, room, rname, text), hop)
608+
case _ _:
609+
B.Step.none()
610+
611+
def Bots.shared.set(same: Bool, +room: String, +rname: String, +shown: String, +now: Nat) -> List<&2, M.Change>:
612+
match same:
613+
case True{}:
614+
Nil{}
615+
case False{}:
616+
[M.RoomSet{room, rname, shown, now}]
617+
618+
# a room post names a room that is not a direct one, with its members
619+
def Bots.shared.is(+o: J.Json) -> Bool:
620+
Bool.and(Bool.not(String.is_empty(J.Json.get_str(o, "room", ""))),
621+
Bool.and(Bool.not(String.starts_with(J.Json.get_str(o, "room", ""), "dm:")), BT.Maybe.is(J.Json, J.Json.get(o, "members"))))
622+
472623
# Routines
473624
# --------
474625

@@ -664,6 +815,72 @@ def Bots.member.of.k(remote: Bool, m: Maybe<&2, BT.Bot>, +x: String) -> String:
664815
def Bots.member.of(+s: M.State, +x: String) -> String:
665816
Bots.member.of.k(Bots.remote(x), BT.World.find(M.State.bots(s), x), x)
666817

818+
def Bots.member.in.k(remote: Bool, sender: Bool, +s: M.State, +peer: String, +x: String) -> String:
819+
match remote sender:
820+
case False{} _:
821+
Bots.member.of(s, x)
822+
case True{} True{}:
823+
Bots.at.name(x) ++ "@" ++ peer
824+
case True{} False{}:
825+
x
826+
827+
# a member as the machine `peer` named it, as this machine names it: a bare
828+
# name is a bot here (its id, "" when there is none), "name@" a bot on
829+
# `peer`, "name@machine" a bot elsewhere
830+
def Bots.member.in(+s: M.State, +peer: String, +x: String) -> String:
831+
Bots.member.in.k(Bots.remote(x), String.is_empty(Bots.at.peer(x)), s, peer, x)
832+
833+
def Bots.members.in(xs: List<&2, J.Json>, +s: M.State, +peer: String) -> List<&2, String>:
834+
match xs:
835+
case Nil{}:
836+
Nil{}
837+
case x <> t:
838+
Bots.member.in(s, peer, J.Json.as_str(Some{x}, "")) <> Bots.members.in(t, s, peer)
839+
840+
# the members a linked machine names, as ids and name@machine here; at most
841+
# Bots.room.cap()
842+
def Bots.shared.members(+s: M.State, +peer: String, +o: J.Json) -> List<&2, String>:
843+
Bots.named(Bots.members.in(List.take(&2, J.Json, J.Json.elems(J.Json.get_obj(o, "members")), Bots.room.cap()), s, peer))
844+
845+
# the bots here a post is for hear it, when they are members
846+
def Bots.shared.wake(tos: List<&2, String>, +i: Nat, +s: M.State, +now: Nat, +ms: List<&2, String>, +from: String, +room: String, +rname: String, +text: String, +hop: Nat) -> B.Step:
847+
match tos:
848+
case Nil{}:
849+
B.Step.none()
850+
case +x <> t:
851+
Step.cat(Bots.shared.one(BT.Room.has(ms, Bots.member.of(s, x)), BT.World.find(M.State.bots(s), x), s, now, i, from, room, rname, text, hop),
852+
Bots.shared.wake(t, 1n+i, s, now, ms, from, room, rname, text, hop))
853+
854+
def Bots.shared.go(ok: Bool, +s: M.State, +now: Nat, +room: String, +rname: String, +ms: List<&2, String>, +from: String, +tos: List<&2, String>, +text: String, +hop: Nat) -> B.Step:
855+
match ok:
856+
case True{}:
857+
B.Step.before(List.append(&2, M.Change, Bots.shared.set(Bots.room.same(BT.World.room(M.State.bots(s), room), rname, BT.Room.members.show(ms)), room, rname, BT.Room.members.show(ms), now),
858+
[M.RoomPosted{B.Hub.id("p", now, B.Hub.seq(s)), room, from, text, hop, now}]), Nil{},
859+
Bots.shared.wake(tos, 0n, s, now, ms, from, room, rname, text, hop))
860+
case False{}:
861+
B.Step.none()
862+
863+
# a post in a shared room from a linked machine: the room is kept here with
864+
# its members as this machine names them, the post logged once, and the
865+
# bots here it is for hear it. Nothing is passed on.
866+
def Bots.shared(+s: M.State, +now: Nat, +peer: String, +o: J.Json) -> B.Step:
867+
Bots.shared.go(Bool.and(Nat.is_le(J.Json.get_nat(o, "hop", 0n), BT.Hop.max()),
868+
Bots.room.open(BT.World.room(M.State.bots(s), J.Json.get_str(o, "room", "")), s, Bots.peer.idm(Bots.peer(s, peer)))),
869+
s, now, J.Json.get_str(o, "room", ""), J.Json.get_str(o, "roomName", "room"), Bots.shared.members(s, peer, o),
870+
J.Json.get_str(o, "from", "") ++ "@" ++ peer, Bots.tos(o), J.Json.get_str(o, "text", ""), J.Json.get_nat(o, "hop", 0n))
871+
872+
def Bots.incoming.by(shared: Bool, +s: M.State, +now: Nat, +peer: String, +o: J.Json) -> B.Step:
873+
match shared:
874+
case True{}:
875+
Bots.shared(s, now, peer, o)
876+
case False{}:
877+
Bots.incoming.dm(s, now, peer, o)
878+
879+
# a message from a bot on a linked machine: in a shared room, or to one bot
880+
# here
881+
def Bots.incoming(+s: M.State, +now: Nat, +peer: String, +o: J.Json) -> B.Step:
882+
Bots.incoming.by(Bots.shared.is(o), s, now, peer, o)
883+
667884
def Bots.members.go(xs: List<&2, String>, +s: M.State) -> List<&2, String>:
668885
match xs:
669886
case Nil{}:

0 commit comments

Comments
 (0)