From cf63ae3e33967940d986f5e79d8a26840768d4a2 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:00:05 +0100 Subject: [PATCH 1/6] Inspectors guide: access and containers per frontend, enabling them with the system browser, the guard of the environment and the contract of the application's callbacks (#686) --- docs/DEVELOPER_ENVIRONMENT.md | 62 +++++++++++++++++++++++++++++++---- docs/WATCHES.md | 4 ++- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/docs/DEVELOPER_ENVIRONMENT.md b/docs/DEVELOPER_ENVIRONMENT.md index cb550ae3..1b92df57 100644 --- a/docs/DEVELOPER_ENVIRONMENT.md +++ b/docs/DEVELOPER_ENVIRONMENT.md @@ -450,13 +450,30 @@ environment's `mount`, not to the embedded bootstrap page): with the mouse or with the arrow keys on its title bar), whose frame opens no connection of its own and borrows the page's bridge, since the adapters give the proof of the view to the main frame only. **Detach** moves the panel to a window of its own, and the next opening of that panel goes there again; **Attach**, in that window, brings it back to the floating - window, where it then opens: + window, where it then opens. The containers follow the access each frontend actually gives, never its name alone: - | Frontend of the page | Detached container | - | --- | --- | - | WebKitGTK, WKWebView, WebView2 | a native secondary window (`webfront.inspector.openWindow`, `openSecondaryWindow`), which carries the proof of the view; opening the panel again brings it to the front, and closing the main window closes it | - | System browser with `trustLoopbackClients` | a tab of the system browser (`window.open`), with its own loopback connection; when the pop-up blocker refuses it, the panel stays in the floating window and a message offers to try again; the tab closes when the page that opened it is gone | - | CEF, or an embedded view without secondary windows | none: Detach is greyed, with the reason in its tooltip (`window.open` there would open a frame without the proof) | + | Frontend of the page | Inspectors | Panel opens in | Detached container | + | --- | --- | --- | --- | + | WebKitGTK, WKWebView, WebView2 | admitted: the view proves itself | the floating window | a native secondary window (`webfront.inspector.openWindow`, `openSecondaryWindow`), which carries the proof of the view; opening the panel again brings it to the front, and closing the main window closes it | + | CEF | admitted: the view proves itself | the floating window | none: CEF opens no secondary window, and `window.open` there would open a frame without the proof; Detach is greyed, with the reason in its tooltip | + | System browser (default of `webfront::WebFront`) | **refused by default**: the entries are greyed and say how to enable them | none | none | + | System browser with `trustLoopbackClients` | admitted: any loopback page | the floating window | a tab of the system browser (`window.open`), with its own loopback connection; when the pop-up blocker refuses it, the panel stays in the floating window and a message offers to try again; the tab closes when the page that opened it is gone | + + `NativeOrBrowserFrontend` behaves as its native engine while it opens a native window, and as the system browser once it + falls back to it. A custom frontend gives the proof only if it implements the `open(..., const ViewLaunch&)` overload and + declares `providesViewProof()` ([frontends](FRONTENDS.md#view-association)). + + **Enabling the inspectors with the system browser.** Prefer an embedded frontend (`webfront::NativeApp`, or + `WebFrontWithFrontend<...>`): its view proves itself and nothing else is admitted. Otherwise, set `trustLoopbackClients` + in a development build only, on a machine with one user and no local proxy or tunnel: + + ```cpp + webfront::BasicDeveloperApp<> app{"my-project", {.trustLoopbackClients = true}}; // system browser + app.bootstrap("developer"); + ``` + + Every loopback connection is then a local client: it reads the inspectors, and it also passes the `ui.isLocalClient()` + check of the application's own callbacks (below). The option is the application's choice; the environment never sets it. Attach brings the panel back to the page that detached it only, even when other pages of the application are open on the same origin. A page keeps its identifier across a reload through Web Locks; in a browser without them, a reloaded @@ -532,6 +549,39 @@ or the tab first). This is a layout, not a sandbox (see [limitations](#dependencies-storage-and-limitations)). A page that mounts the terminal itself (`runtime.mount`) with a preview frame gets the same layout; `workspace: false` keeps the page as it is. +### The inspectors and the application's callbacks + +The inspectors exist only in the developer environment: `bootstrap("developer")` registers the reserved +`webfront.inspector.*` functions, an application without it has none, and a distribution build +(`WEBFRONT_DISTRIBUTION=ON`) compiles none of them; `app.watch` then returns an empty handle. They read what the application +shows them and write only the [watched values](WATCHES.md#writing-a-value) that the application made writable. They are +not a sandbox: an admitted page shares the authority of the bridge. + +When the frontend proves its view, WebFront guards its own entry points that change what the view runs or what the server +serves: the project writes (`webfront.developer.begin`, `beginNew`, `write`, `commit`, `commitGroup`, `abort`) and +starting or cancelling a terminal command (`webfront.commands.start`, `cancel`) require an admitted page, like the +inspectors, and are refused with `webfront.inspector.denied` otherwise. The reads of the project and +`webfront.commands.list` keep the origin check. With the system browser no view is proven, so these operations stay open +to every connection that the [content policy](CONTENT_POLICY.md) admits. + +**The callbacks of the application are its own contract** (ADR-027, section 2). WebFront does not know what a +`cppFunction` does and does not guard it. A callback that writes the project or what the server serves, starts a process, +or exposes a state the application would not show to an unproven client takes the calling `UI` as its first parameter and +checks it before acting: + +```cpp +app.cppFunction("app.saveScript", [&](webfront::NativeApp::UI ui, std::string text) { + if (!ui.isLocalClient()) // not the application's view, nor a trusted loopback page + throw std::runtime_error("app.saveScript requires the application's view"); // the Promise rejects + saveScript(text); +}); +``` + +Without that check, such a callback opens again the door the guard above closes: a local process could call it, then let +the proven view load what it wrote. The **Caller** column of the Functions panel shows which callbacks receive the `UI`, +and so can make the check. An agent reads the same state through MCP, with the `state` grant, and never writes it +([Developing with an agent](DEVELOPING_WITH_AGENTS.md#reading-the-running-application-webfrontstate)). + ## The terminal and the application's commands The terminal of the environment (developer distribution 0.5.0, #308) is the only WebFront terminal; it is served diff --git a/docs/WATCHES.md b/docs/WATCHES.md index 47df58d9..36bec68d 100644 --- a/docs/WATCHES.md +++ b/docs/WATCHES.md @@ -33,7 +33,9 @@ call, which the panel shows. This is a development tool, not a sandbox. Only a page that is admitted reads or writes a watch: the view of an embedded frontend, or a loopback page when the application chose `trustLoopbackClients` (see -[the menu and the inspectors](DEVELOPER_ENVIRONMENT.md)). +[the menu and the inspectors](DEVELOPER_ENVIRONMENT.md#the-page-interface-and-floating-terminal)). A watch is +read-only unless its source writes, and only the panel writes it: the [MCP reads](#reading-a-watch-from-an-agent) never do. +The application's own callbacks follow [their own contract](DEVELOPER_ENVIRONMENT.md#the-inspectors-and-the-applications-callbacks). ## Sources: one operation, one access From 576b607cc588f00a2df1ce0587dc93162c23be57 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:00:05 +0100 Subject: [PATCH 2/6] Tell agents that a callback that acts checks ui.isLocalClient(), and have AgentGuidance check it (#686) ADR-027, section 2: in docs/DEVELOPING_WITH_AGENTS.md, the starters' agent guide and the guide of the inspectors. bundles.json regenerated by wf for the agent guide. --- AGENTS.md | 2 +- docs/DEVELOPING_WITH_AGENTS.md | 23 +++++++++++++++++++++++ js/developer/environment/bundles.json | 2 +- js/developer/starters/agent-guide.md | 4 ++++ test/cmake/CheckAgentGuide.cmake | 14 ++++++++++++++ 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 9653fbef..d1bf5219 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -109,7 +109,7 @@ The HTTP server connections and the WebSocket read/write loops are coroutines (` Filesystems implement `open(std::filesystem::path) -> std::optional` and are composed with `fs::Multi<>`; the first provider returning a file wins. `NativeDebugFS` is development-only and must not be presented as a production sandbox. -Bridge calls are asynchronous, and the two directions differ. `webFront.cppFunction(name)(...)` always returns a JavaScript Promise (also for `void` callbacks), so tests and examples must await or handle it. On the C++ side, only the untyped `ui.jsFunction(name)(...)` is fire-and-forget; `ui.jsFunction(name)(...)` returns a `std::future`. `ui.jsFunctionSender(name)(...)` owns encoded arguments and dispatches at sender start; co_await propagates the receiver stop token. Cancellation completes stopped locally and discards a late reply; JavaScript continues running. See `docs/ASYNC_EXECUTION.md` for lifetime and completion-thread rules. On missing functions, callback exceptions, malformed returns, and disconnects, a C++ future completes exceptionally (`get()` throws) and a JavaScript Promise rejects. Result types are limited to the bridge's scalar, string, tuple, numeric typed-array, packed `std::bitset` and list types. A list is a `std::vector` of strings, tuples, typed arrays or bitsets (JavaScript: `webFront.list(items)` of strings, typed arrays or tuples, received as an `Array`; numbers go as typed arrays; a plain short array is a tuple that a `std::vector` destination also accepts); elements are homogeneous and lists do not nest. Bitsets use `Uint8Array` with exactly `ceil(N / 8)` bytes, least-significant bit first in each byte, and zero high padding bits; decoding rejects wrong types, sizes and padding. JavaScript must know the logical bit count from the application contract. Strings of 65,536 bytes or more use the `longString` code (32-bit length), chosen by the encoder; the only bound is the buffers policy's `maxMessageSize` (64 MiB by default, JavaScript's `maxMessageBytes`): beyond it C++ throws `std::length_error` when it encodes, which rejects the Promise when it is a callback's result, and JavaScript throws a `RangeError` before sending. A `cppFunction` callback may return a sender (a `webfront::ResultTask` coroutine): the Promise settles when it completes, the browser's disconnection stops it and drops its answer, and shutdown joins it. Callback signatures are deduced for ordinary lambdas, function pointers, and `std::function`; generic lambdas and overloaded call operators need explicit `cppFunction`. Do not write tests or documentation that present JavaScript calls as fire-and-forget, imply an untyped C++ call returns a value, or suggest that richer objects cross the bridge (only the types above do). Registered callback objects must outlive their registration through owned storage; never capture a forwarding-reference parameter by reference in a stored handler. +Bridge calls are asynchronous, and the two directions differ. `webFront.cppFunction(name)(...)` always returns a JavaScript Promise (also for `void` callbacks), so tests and examples must await or handle it. On the C++ side, only the untyped `ui.jsFunction(name)(...)` is fire-and-forget; `ui.jsFunction(name)(...)` returns a `std::future`. `ui.jsFunctionSender(name)(...)` owns encoded arguments and dispatches at sender start; co_await propagates the receiver stop token. Cancellation completes stopped locally and discards a late reply; JavaScript continues running. See `docs/ASYNC_EXECUTION.md` for lifetime and completion-thread rules. On missing functions, callback exceptions, malformed returns, and disconnects, a C++ future completes exceptionally (`get()` throws) and a JavaScript Promise rejects. Result types are limited to the bridge's scalar, string, tuple, numeric typed-array, packed `std::bitset` and list types. A list is a `std::vector` of strings, tuples, typed arrays or bitsets (JavaScript: `webFront.list(items)` of strings, typed arrays or tuples, received as an `Array`; numbers go as typed arrays; a plain short array is a tuple that a `std::vector` destination also accepts); elements are homogeneous and lists do not nest. Bitsets use `Uint8Array` with exactly `ceil(N / 8)` bytes, least-significant bit first in each byte, and zero high padding bits; decoding rejects wrong types, sizes and padding. JavaScript must know the logical bit count from the application contract. Strings of 65,536 bytes or more use the `longString` code (32-bit length), chosen by the encoder; the only bound is the buffers policy's `maxMessageSize` (64 MiB by default, JavaScript's `maxMessageBytes`): beyond it C++ throws `std::length_error` when it encodes, which rejects the Promise when it is a callback's result, and JavaScript throws a `RangeError` before sending. A `cppFunction` callback may return a sender (a `webfront::ResultTask` coroutine): the Promise settles when it completes, the browser's disconnection stops it and drops its answer, and shutdown joins it. Callback signatures are deduced for ordinary lambdas, function pointers, and `std::function`; generic lambdas and overloaded call operators need explicit `cppFunction`. Do not write tests or documentation that present JavaScript calls as fire-and-forget, imply an untyped C++ call returns a value, or suggest that richer objects cross the bridge (only the types above do). Registered callback objects must outlive their registration through owned storage; never capture a forwarding-reference parameter by reference in a stored handler. Names starting with `webfront.inspector.` are reserved to the inspectors (ADR-027). WebFront guards only its own reserved functions: an example or test callback that writes the project or what the server serves, starts a process or exposes a sensitive state takes the calling `UI` and checks `ui.isLocalClient()`, and documentation must not present the application's callbacks as guarded by WebFront. The embedded `WebFront.js` wire format and C++ `Messages.hpp` implementation must evolve together. Any protocol change requires encoding/decoding unit tests and a real-browser regression test. diff --git a/docs/DEVELOPING_WITH_AGENTS.md b/docs/DEVELOPING_WITH_AGENTS.md index 92ad5b62..df298c24 100644 --- a/docs/DEVELOPING_WITH_AGENTS.md +++ b/docs/DEVELOPING_WITH_AGENTS.md @@ -36,6 +36,29 @@ not how the interface is shipped. An application that serves a document root wit line on stderr at its first `show()` saying so. Only [scalars, strings, tuples, typed arrays, bitsets and lists](../README.md#contracts-and-limitations) cross the bridge. +### A callback that acts checks its caller + +WebFront guards its own functions, not the application's: it does not know what a `cppFunction` does. **A callback that +writes the project or what the server serves, starts a process, or exposes a state the application would not show to any +client takes the calling `UI` as its first parameter and checks `ui.isLocalClient()` before acting** (ADR-027, section 2). +`isLocalClient()` is true for the view of an embedded frontend, which proves itself, and for a loopback page only when the +application sets `trustLoopbackClients`. + +```cpp +app.cppFunction("app.saveScript", [&](webfront::NativeApp::UI ui, std::string text) { + if (!ui.isLocalClient()) + throw std::runtime_error("app.saveScript requires the application's view"); // the Promise rejects + saveScript(text); +}); +``` + +The calling `UI` is not a parameter on the JavaScript side: the page still calls `webFront.cppFunction('app.saveScript')(text)`. +A callback that only computes and returns a value needs no check. Without it, a callback that acts can be called by a +local process that forges the page's origin (the origin check alone cannot tell it from the page), then the proven view +may load what it wrote. The Functions panel of the +[inspectors](DEVELOPER_ENVIRONMENT.md#the-inspectors-and-the-applications-callbacks) shows which callbacks receive the +caller. + ## The development loop `wf create ` takes the name of a **starter**, not the name of your project: `app` is a minimal TypeScript diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index 67fb4afb..5e94a984 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -50,7 +50,7 @@ "js/developer/environment/embed.mjs": "3d3b5036c34c23a1fa0512c1eea42f26d5f30e054545b61fe83a5c378b74e09e", "examples/life/ui/main.tsx": "7b1ff285d2a6b8f897ef12df872886e61ef4b24e2358a9423b76b38ab75c85d3", "examples/life/ui/style.css": "ac13886f0aea2adfdca21c487467a8f6e647feffb88c51d81766e628584ea5cd", - "js/developer/starters/agent-guide.md": "b3c846e009195238f23c0d96e6a651fdd2055b0138222f248f7675aeabee4a93", + "js/developer/starters/agent-guide.md": "b4d67494d27a575443726cfc27cc67c26c0e70d57909bc72ee6fac7623d3efb7", "js/developer/starters/app/main.ts": "b4f43fd3cd9136d86dd3e6e89e29092ddf3b67066c2cc08d78078d15a445054e", "js/developer/starters/app/style.css": "6a042eff15ee9b1603c95d3e4e974a03876e1c365389553e84eb7c472aded180", "js/developer/tools/distribution.json": "06c5e81e5fbbe3407488840646fb8dd1b36b540fca43dd85dcd5d7342261a36a", diff --git a/js/developer/starters/agent-guide.md b/js/developer/starters/agent-guide.md index 10130293..196ddd0f 100644 --- a/js/developer/starters/agent-guide.md +++ b/js/developer/starters/agent-guide.md @@ -13,6 +13,10 @@ backend. own window. `wf` installs npm packages, compiles TS/TSX, type-checks and runs Jasmine specs: no Node.js, no package manager. - Hand-written HTML with classic `