Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/lld/17-preview-pane.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,9 @@ Design on the canvas "Genie: browsing the preview pane" (8 boards), approved the
The bridge commands (`livepreview/bridge_cmd.go`) dispatch real events: the pointer events carry `pointerId`, `pointerType: "mouse"`, `isPrimary`, `buttons` (1 while pressed), so a game that checks them accepts them, followed by the mouse events. Added: `preview_drag` (`ref`, `dx`, `dy`, optional `x`, `y`, `steps`; press, small moves, release, on the part so that a game that listens on the element or the window gets them), `x` and `y` on `preview_click` (a point of a canvas), `space: "drawing"` (the numbers are in the canvas's own drawing coordinates, scaled by its size on screen), `preview_find` (by CSS `selector`, by `query` text, or by `left` and `top` point of the visible page; each match gets an e-number usable with click, drag and inspect, and its place and size in page pixels), `preview_read` with `boxes: true` (place and size on every part), a `<canvas>` of 40 pixels or more as a part with its size and its drawing size, and `page` and `drawing` in the answer of `preview_inspect`. What is drawn on a canvas cannot be read: the model is told to look in the game's own code or state (`preview_eval`) for where things are. Tests: `agent-protocol.test.ts` (parsing), `preview-bridge.test.mjs`, browser `genie-preview-agent.spec.js` (a game that listens for pointer events only, a scaled canvas, find, boxes).

**Hidden files do not reload the page** (2026-11-19). `Preview.changed(path)` (`27-preview.js`) reloads a previewed page when a file under its folder is written. A file or folder whose name starts with a dot is skipped. A page in the home folder was reloaded on every step of a Genie task, because the chat files in `.genie/chats` are written there: the bridge lost its numbered parts (the log showed `stale_ref_reload` on every click) and the game restarted. Test: `preview.spec.js` "a hidden file written next to the page".

## Routing after a session moves (2026-11-23)

The gateway sends `/preview/<token>/...` to the node that runs the workspace the token names. That node used to come only from the route table (`RouteHome`), which the workers fill with their announcements and empty with a "closed" announcement. A session that moved (to a worker, back to the gateway, after a worker was away) can leave the old entry behind, so the Preview kept asking the node that no longer ran the program: "Nothing is running yet" with the program listening on the other node (reported with logs from a gateway and a worker). `Router.homeBackend` now asks the registry first (`SessionRegistry.HomeOwner`: the live session that uses the home), drops a route entry that disagrees with it, and falls back to the route table only when no live session uses the home. The same helper serves the `homedir` of a shared session's file requests (`routed`). Test: `TestThePreviewFollowsTheSessionWhenItMovedAndTheRouteIsStale`.

An admin's REPL listens on the machine's real network, where the site itself is, so the Preview refuses the server's own port for it (`isOwnPort`, `errOwnPort`) instead of showing the site in the frame.
18 changes: 16 additions & 2 deletions src/gateway/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,20 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

// homeBackend is the node that runs the workspace now. The live session that uses the home says it: a session that moved (a worker away
// for a while, an admin moving it, or back to the gateway) is placed again, and the registry follows it. The route table is only what the
// workers announced, and an entry stays when a move leaves no "closed" announcement behind (reported: the Preview kept asking the worker that
// no longer ran the program, "Nothing is running yet"), so it is the fallback, and an entry that disagrees with the registry is dropped.
func (rt *Router) homeBackend(home string) (string, bool) {
if id, ok := rt.registry.HomeOwner(home); ok {
if stale, found := rt.routes.Lookup(RouteHome, home); found && stale != id {
rt.routes.Delete(RouteHome, home, stale)
}
return id, true
}
return rt.routes.Lookup(RouteHome, home)
}

// servePreview passes a request of the Preview (/preview/<token>/...) to the worker that runs the workspace the token names,
// where the program is: the frame carries no cookie, so the token is all there is to say whose it is. The worker checks the
// token itself. It reports false when the workspace is the gateway's own (or unknown), and the gateway answers as before.
Expand All @@ -406,7 +420,7 @@ func (rt *Router) servePreview(w http.ResponseWriter, r *http.Request, rel strin
if home == "" {
return false
}
id, ok := rt.routes.Lookup(RouteHome, home)
id, ok := rt.homeBackend(home)
if !ok || id == LocalID {
return false
}
Expand Down Expand Up @@ -576,7 +590,7 @@ func (rt *Router) routed(r *http.Request) (string, bool) {
}
}
if home := q.Get("homedir"); home != "" {
if b, ok := rt.routes.Lookup(RouteHome, home); ok {
if b, ok := rt.homeBackend(home); ok {
return b, true
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/gateway/router_preview_move_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gateway

import (
"testing"
"time"

"livepreview"
)

// A session that moved back to the gateway: the route table still names the worker that held the workspace before (no "closed" announcement
// came), but the registry says where the session runs now. The Preview follows the registry.
func TestThePreviewFollowsTheSessionWhenItMovedAndTheRouteIsStale(t *testing.T) {
site := &recorder{}
a, b := &named{id: "worker-a"}, &named{id: "worker-b"}
rt := newTestRouter(site, "/", &countingPicker{id: "worker-a"})
rt.AddBackend(a)
rt.AddBackend(b)
home := "/home/openrepl/donadjdc"
tok, _ := livepreview.NewToken("secret", home, time.Now(), time.Hour)
// the worker announced the home; then the session moved to the gateway
rt.Routes().Set(RouteHome, home, "worker-b")
rt.registry.Create(Identity{Key: "u:donadjdc", UID: "donadjdc", Home: home}, LocalID)
do(rt, "GET", "/preview/"+tok+"/p/8000/", nil)
if b.hits != 0 || len(site.paths) != 1 {
t.Fatalf("the Preview went to the old worker: b=%d site=%v", b.hits, site.paths)
}
if _, stale := rt.Routes().Lookup(RouteHome, home); stale {
t.Error("the stale route must be dropped")
}
// and the other way: the session runs on a worker, the table knows nothing
rt2 := newTestRouter(&recorder{}, "/", &countingPicker{id: "worker-a"})
w := &named{id: "worker-a"}
rt2.AddBackend(w)
rt2.registry.Create(Identity{Key: "u:donadjdc", UID: "donadjdc", Home: home}, "worker-a")
do(rt2, "GET", "/preview/"+tok+"/p/8000/", nil)
if w.hits != 1 {
t.Errorf("the Preview did not go to the worker that runs the session: %d", w.hits)
}
}
12 changes: 12 additions & 0 deletions src/server/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ func previewDialer(home string) livepreview.Dialer {
return nil, lastErr
}
if admin {
// an admin's REPL listens on the machine's real network, where the site itself is: its own port is not a program to preview
if isOwnPort(port) {
return nil, errOwnPort
}
var d net.Dialer
return d.DialContext(ctx, "tcp", fmt.Sprintf("127.0.0.1:%d", port))
}
Expand Down Expand Up @@ -301,6 +305,14 @@ func noteOwnPort(port string) {
}
}

var errOwnPort = errors.New("this is the port of the site itself, not of a program of yours")

func isOwnPort(port int) bool {
ownPortsMu.Lock()
defer ownPortsMu.Unlock()
return ownPorts[port]
}

func withoutOwnPorts(in []int) []int {
ownPortsMu.Lock()
defer ownPortsMu.Unlock()
Expand Down
9 changes: 9 additions & 0 deletions src/server/preview_ports_egress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ func TestThePreviewDoesNotListThePortOfTheServerItself(t *testing.T) {
t.Fatalf("got %v", got)
}
}

// the Preview of an admin dials the real network, where the site is: its own port is refused
func TestTheServersOwnPortIsRefusedForThePreview(t *testing.T) {
noteOwnPort("8080")
defer func() { ownPortsMu.Lock(); delete(ownPorts, 8080); ownPortsMu.Unlock() }()
if !isOwnPort(8080) || isOwnPort(8000) {
t.Error("only the server's own port is its own")
}
}
Loading